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

R Markdown is an enhanced form of Markdown coupled with in-line R code. Curious? Learn more here.
Warp Terminal

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

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.

Select Install Packages option under Tools menu in RStudio
Select Install Packages option under Tools menu (click to enlarge image)

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

Install RMarkdown Package by searching it and pressing install button on the new package install dialog box
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.

Create a new RMarkdown Document from File menu
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.

Provide the title and other details for new document in rmarkdown
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 Block Proper Syntax
Heading # Level 1
## Level 2
### Level 3

Level 1
=======

Level 2
-------
Emphasis *Italics*
_Italics_

**Bold**

__Bold__
List Unordered List
* Item
* Item
+ Sub
+ Sub

Ordered List
1. Item
2. Item
+ Sub
+ Sub
Code Chunk Normal Code Block

```
Code Goes Here
```

R Code Block

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


Inline `code`
Links Plain 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 |
Equations In line Equations: $Equations$

Display Equations: $$Equations$$
Images Without Caption: ![](Link-to-Image)

With Caption : ![optional caption text](Location-of-image)
Block Quotes > Type your Block Quotes
Misc Super 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
---------------
various types of heading levels in rmarkdown file
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
order and unordered list example

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.

Format text within a paragraph

There are several ways to format text.

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~~

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:

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)

Tables

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

|Column|Column|Column|
| --- | --- | --- |
|Item|Item|Item|
|Item|Item|Item|
πŸ“‹
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)

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

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$$
adding equations in R Markdown document
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:

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

About the author
Sreenath

Sreenath

Linux tinkerer by day, Lord of the Rings fanatic by night. When I'm not busy conquering the command line, you can find me kicking back and watching some football while quoting JRR Tolkien

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

itsfoss happy penguin

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.