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.
Suggested Read 📖
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.
On the new dialog box, search for rmarkdown and install it.
Once installed, you can start a new rmarkdown document by selecting File > New > RMarkdown.
This will prompt you to add some information regarding the document (metadata for the file). Fill those up.
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
---------------
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
Suggested Read 📖
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:
Links
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|
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$$
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.
0 Commentaires