Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

You probably already know about the lightweight Markdown markup language. Refer to our Markdown guide, if you’re new to the concept. Overall, it is a simple and effective language for creating plain-text documents.

However, Markdown may not be enough to make detailed reports or technical documents.

Hence, R Markdown as an interactive file format came into existence back in 2014 thanks to packages like knitr and Pandoc. It combines plain text with in-line R code, helping you make a dynamic document.

To create R Markdown documents, you can use various IDEs and extensions to make it possible. However, the official IDE that helps you do it is RStudio. So, in this article, we will focus on learning R Markdown syntax using RStudio.

💡
If you did not know, R programming language is used for statistical computing, graphics representation, and reporting.

Suggested Read 📖

How to Install and Use R on Ubuntu
Brief: This tutorial teaches you to install R on Ubuntu. You’ll also learn how to run your first R program in Ubuntu using various methods. R, together with Python, is the most commonly used programming language for statistical computing and graphics, making it easy to work with data. With
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

Setting RStudio

RStudio makes it easy to work with R Markdown by its setup process. You just need to install a package, and you are done for the most part!

Once you have RStudio installed, head to the Tools menu and select the Install Packages option.

Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
Select Install Packages option under Tools menu (click to enlarge image)

On the new dialog box, search for rmarkdown and install it.

Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
Install RMarkdown Package (click to enlarge image)
💡
To use code chunks like python, you need to install additional packages. RStudio will prompt you to install the required packages when you try to include them in your document.

Once installed, you can start a new rmarkdown document by selecting File > New > RMarkdown.

Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
Create a new RMarkdown Document (click to enlarge image)

This will prompt you to add some information regarding the document (metadata for the file). Fill those up.

Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
New document in rmarkdown (click to enlarge image)

Or you can create an empty document to start fresh.

RMarkdown Syntax

Since it is just “enhanced Markdown,” most syntax remains the same.

There would be some differences when you add things not usually supported with Markdown, like tables, math equations, code chunks, etc.

Here’s a quick summary of what we will be covering:

Name of the RMarkdown BlockProper Syntax
Heading# Level 1
## Level 2
### Level 3

Level 1
=======

Level 2
——-

Emphasis*Italics*
_Italics_

**Bold**

__Bold__

ListUnordered List
* Item
* Item
+ Sub
+ Sub

Ordered List
1. Item
2. Item
+ Sub
+ Sub

Code ChunkNormal Code Block

“`
Code Goes Here
“`

R Code Block

“`{r}
R CODE
“`
You can use other languages also.

Inline `code`

LinksPlain Link: Paste the URL
Link with Caption: [Text](URL_Address)
Link to a section: [Text](#Name-of-section)
Table| Column | Column | Column |
| —— | —— | —— |
| Item | Item | Item |
| Item | Item | Item |
EquationsIn line Equations: $Equations$

Display Equations: $$Equations$$

ImagesWithout Caption: ![](Link-to-Image)

With Caption : ![optional caption text](Location-of-image)

Block Quotes> Type your Block Quotes
MiscSuper Script : Text^Superscript^

Horizontal rule or Page Break:

========= or ———-

For Manual Line break, end line with 2+ spaces

The YAML Header

At the top of a Rmarkdown document, there is a YAML header enclosed within two ---. This block usually contains a title, author, date, and the file type you want to output, defining the final look of the document.

The file type is either HTML, PDF, or Word.

---
title: "Sample"
author: "It's FOSS"
date: "2023-02-08"
output: pdf_document
---

This can be added while setting the new document in RStudio, which is shown in the above section.

Heading

In R Markdown, we can give heading in two different methods. Either we can use the # character for different levels of heading like:

# Heading Level 1
## Heading Level 2
### Heading Level 3
#### Heading Level 4
##### Heading Level 5
###### Heading Level 6

Or, = and - for level 1 and 2 headings, respectively.

Level 1 Heading
===============

Level 2 Heading
---------------
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
Heading levels (click to enlarge image)

Lists

There are two types of Lists, the first one is an Unordered list, or you could call them bullet points:

* Item 1
* Item 2
		+ Sub 1
		+ Sub 2
* Item 3

And the second one is the Ordered list, which is the numbered type:

1. Item 1
2. Item 2
		+ Sub 1
		+ Sub 2
3. Item 3
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

Suggested Read 📖

Read and Organize Markdown Files in Linux Terminal With Glow
Glow is a CLI tool that lets you render Markdown files in the Linux terminal. You can also organize Markdown files with it.
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

Format text within a paragraph

There are several ways to format text.

Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

You can add emphasis to the text like italics or bold using:

  • Italics: Place the text in between single asterisks or single underscore
  • Bold: Place the text in between double asterisks or double underscores.
*This is Italicized text*
_This is Italicized text_

**This is Bold Text**
__This is Bold Text__

You can explore on this using our resource on how to add bold and italic text in Markdown.

If you want to add superscript to a text, place the text that should be superscript in between ^ symbol.

Normal Text^super_script^

Or, if you want to add text strike-through, place the text in between two ~~ symbol.

~Strike Through this~~
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

Adding Code Chunks

Embedding code is the primary purpose of R Markdown. It allows us to add codes in several ways.

Adding Normal code block.

If you want to add a normal code block to separate it from other text, use the syntax below:

```
Your Code Goes Here
```

You can also try adding code blocks with syntax highlighting.

You should append the language in curly braces if you want to add code and embed its output to the document:

```{Language}
Your Code Goes Here
```

Or, you can add inline codes by placing the respective text between ` symbols.

	The `code` is a code

Here’s how it should look like:

Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
Code Blocks (click to enlarge image)

To add a link as plain text, just paste the link as it is in a line.

https://itsfoss.com

Or, to make a text hyperlink, use the syntax:

[Text](URL Address)

Another way to add a link is, when you want to link to a section of the page. In this case, use the syntax:

[Text](#Name-of-section)
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

Tables

The syntax for adding tables is similar to that of markdown.

|Column|Column|Column|
| --- | --- | --- |
|Item|Item|Item|
|Item|Item|Item|
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
📋
Curious to know more? Refer to our guide on creating tables in Markdown.

Images

To add an image, use the syntax:

![](http://example.com/logo.png)

OR

![optional caption text](figures/img.png)
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

Block Quotes

RMarkdown allows you to add block quotes. To use this, use the > (greater than) symbol in front of the line/paragraph you want to quote.

This is a normal text

> This is a Block Quote
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]

If you want to explore more use cases of blockquote, head to our Markdown quotes guide.

Equations

Using RMarkdown, you can add either equations or display complex LaTex equations.

For example:

In line Pythagorean Theorem: $Equation$

Display Equation: $$Equation$$
Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
Adding equations (click to enlarge image)

Horizontal Rule / Page Break

Use three or more asterisks or dashes to add a horizontal rule /page break.

************

------------

If you want to add a manual line break, end that line with two or more spaces.

Summary

R Markdown is Useful (Cheat Sheet)

Whether you are working with scientific reports or want to create any other type of dynamic document, R Markdown is your best bet to make the most out of Markdown.

Here’s a cheat sheet to help you summarize it all:

Beginner's Guide to R Markdown Syntax [With Cheat Sheet]
R Markdown Cheat Sheet

download-circle

💬 Did we miss something that you use with R Markdown? Share your thoughts in the comments down below.

Leave a Comment