Markup Language Cheat Sheet
R Markdown (Allaire et al. 2021) is a plain-text document format consisting of two components: R (or other computing languages) and Markdown. Markdown makes it easy for authors to write a document due to its simple syntax. Program code (such as R code) can be embedded in a source Markdown document to generate an output document directly: when compiling the source document, the program code will be executed and its output will be intermingled with the Markdown text.
4.1 ioslides presentation. To create an ioslides presentation from R Markdown, you specify the ioslidespresentation output format in the YAML metadata of your document. You can create a slide show broken up into sections by using the # and ## heading tags (you can also create a new slide without a header using a horizontal rule (-).For example here is a simple slide show (see Figure 4.1 for. This saves a version of your RMD file that already has data pre-loaded and will open much faster than re-loading data every time you want to knit the file. 5 MARKDOWN FORMATTING RULES. Examples taken from Adam Pritchard’s Markdown Cheatsheet. 6.4 Cheat Sheet. The character # at the beginning of a line means that the rest of the line is interpreted as a section header. The number of #s at the beginning of the line indicates whether it is treated as a section, sub-section, sub-sub-section, etc. Of the document.For instance, Basic Formatting in R Markdown above is preceded by a single #, but Headers at the start of this paragraph was preceded.
R Markdown files usually use the filename extension .Rmd
. Below is a minimal example:
Such a document can be compiled using the function rmarkdown::render()
, or equivalently, by clicking the Knit
button in RStudio. Under the hood, an R Markdown document is first compiled to Markdown through knitr(Xie 2021), which executes all program code in the document. Then the Markdown output document is compiled to the final output document through Pandoc, such as an HTML page, a PDF document, a Word document, and so on. It is important to know this two-step process, otherwise you may not know which package documentation to look up when you have questions. Basically, for anything related to the (R) code chunks, consult the knitr documentation (https://yihui.org/knitr/); for anything related to Markdown, consult the Pandoc documentation (https://pandoc.org).
An R Markdown document typically consists of YAML metadata (optional) and the document body. YAML metadata are written between a pair of ---
to set some attributes of the document, such as the title, author, and date, etc. In the document body, you can mix code chunks and narratives. A code block starts with a chunk header ```{r}
and ends with ```
. There are many possible chunk options that you can set in the chunk header to control the output, e.g., you can set the figure height to 4 inches using ```{r fig.height=4}
. For all possible chunk options, see https://yihui.org/knitr/options/.
Pandoc supports a large variety of output document formats. For blogdown, the output format is set to HTML (blogdown::html_page
), since a website typically consists of HTML pages. If you want other formats, please see Section 2.7. To create an R Markdown post for blogdown, it is recommended that you use the RStudio “New Post” (Figure 1.2) or the function blogdown::new_post()
, instead of the RStudio menu File -> New File -> R Markdown
.
You are strongly recommended to go through the documentation of knitr chunk options and Pandoc’s manual at least once to have an idea of all possibilities. The basics of Markdown are simple enough, but there are many less well-known features in Pandoc’s Markdown, too. As we mentioned in Section 1.5, blogdown’s output format is based on bookdown(Xie 2020a), which contains several other Markdown extensions, such as numbered equations and theorem environments, and you need to read Chapter 2 of the bookdown book (Xie 2016) to learn more about these features.
You can find an R Markdown cheat sheet and a reference guide at https://www.rstudio.com/resources/cheatsheets/, which can be handy after you are more familiar with R Markdown.
Rmd File
With R Markdown, you only need to maintain the source documents; all output pages can be automatically generated from source documents. This makes it much easier to maintain a website, especially when the website is related to data analysis or statistical computing and graphics. When the source code is updated (e.g., the model or data is changed), your web pages can be updated accordingly and automatically. There is no need to run the code separately and cut-and-paste again. Besides the convenience, you gain reproducibility at the same time.