11 KiB
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][1], 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][2] and [Pandoc][3]. 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][4] 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 UbuntuBrief: 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![][5]It's FOSSSergiu![][6]
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][7]
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][8]
💡
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][9]
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][10]
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 3Level 1=======Level 2------- |
| Emphasis | ItalicsItalicsBoldBold |
| List | Unordered List* Item* Item + Sub + SubOrdered List1. Item2. Item + Sub + Sub |
| Code Chunk | Normal Code BlockCode Goes Here
R Code Block{r}R CODE
You can use other languages also.Inline code
|
| Links | Plain Link: Paste the URLLink with Caption: TextLink to a section: Text |
| Table | | Column | Column | Column || ------ | ------ | ------ || Item | Item | Item || Item | Item | Item | |
| Equations | In line Equations: $Equations$Display Equations: $$Equations$$ |
| Images | Without Caption: With Caption : |
| 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][11]
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][12]
Suggested Read 📖
Read and Organize Markdown Files in Linux Terminal With GlowGlow is a CLI tool that lets you render Markdown files in the Linux terminal. You can also organize Markdown files with it.![][13]It's FOSSAbhishek Prakash![][14]
Format text within a paragraph
There are several ways to format text.
![][15]
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][16].
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~~
![][17]
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][18].
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:
![][19]
#### Links
To add a link as plain text, just paste the link as it is in a line.
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:
![][20]
#### Tables
The syntax for adding tables is similar to that of markdown.
Column | Column | Column |
---|---|---|
Item | Item | Item |
Item | Item | Item |
![][21]
📋
Curious to know more? Refer to our guide on
[creating tables in Markdown][22]
.
#### Images
To add an image, use the syntax:
OR
![][23]
#### 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
![][24]
If you want to explore more use cases of blockquote, head to our [Markdown quotes][25] 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][26]
#### 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:
![][27]
[R Markdown Cheat SheetR Markdown Cheat Sheet.pdf145 KBdownload-circle][28]
_💬 Did we miss something that you use with R Markdown? Share your thoughts in the comments down below._
--------------------------------------------------------------------------------
via: https://itsfoss.com/r-markdown/
作者:[Sreenath][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sreenath/
[b]: https://github.com/lkxed/
[1]: https://itsfoss.com/markdown-guide/
[2]: https://www.r-project.org/nosvn/pandoc/knitr.html
[3]: https://itsfoss.comknitr, and Pandoc
[4]: https://itsfoss.com/best-modern-open-source-code-editors-for-linux/
[5]: https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png
[6]: https://itsfoss.com/content/images/wordpress/2019/06/install-r-on-ubuntu.jpg
[7]: https://itsfoss.com/content/images/2023/02/select-install-packages.png
[8]: https://itsfoss.com/content/images/2023/02/install-rmarkdown-1.png
[9]: https://itsfoss.com/content/images/2023/02/new-r-markdown.png
[10]: https://itsfoss.com/content/images/2023/02/new-document-in-rmark.png
[11]: https://itsfoss.com/content/images/2023/02/Headings.png
[12]: https://itsfoss.com/content/images/2023/02/List.png
[13]: https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png
[14]: https://itsfoss.com/content/images/wordpress/2022/01/glow-cli-markdown.png
[15]: https://itsfoss.com/content/images/2023/02/emphasis.png
[16]: https://itsfoss.com/markdown-bold-italic/
[17]: https://itsfoss.com/content/images/2023/02/superescript.png
[18]: https://itsfoss.com/markdown-code-block/
[19]: https://itsfoss.com/content/images/2023/02/code-chunk.png
[20]: https://itsfoss.com/content/images/2023/02/links.png
[21]: https://itsfoss.com/content/images/2023/02/table.png
[22]: https://itsfoss.com/markdown-table/
[23]: https://itsfoss.com/content/images/2023/02/images.png
[24]: https://itsfoss.com/content/images/2023/02/block-quotes.png
[25]: https://itsfoss.com/markdown-quotes/
[26]: https://itsfoss.com/content/images/2023/02/equations.png
[27]: https://itsfoss.com/content/images/2023/02/R-Markdown-Cheat-Sheet.webp
[28]: https://itsfoss.com/content/files/2023/02/R-Markdown-Cheat-Sheet.pdf