Chapter 16 R Markdown
An R Markdown file (.Rmd) is a document that combines
ordinary text
Markdown formatting
R code
output such as tables and graphs
You can use R Markdown to
write and run R code
create PDF files
create HTML files
create slides for presentations
combine your code, results, and explanation in one document
R Markdown can be useful for assignments, reports, notes, and projects.
16.1 Installation
To use R Markdown, you may need to install some packages first. See:
https://bookdown.org/yihui/rmarkdown/installation.html
If you want to knit to PDF, you may also need a LaTeX installation such as TinyTeX.
16.2 Knit a document
You can knit your file by
clicking the Knit button in RStudio, or
using Ctrl + Shift + K
When you knit an R Markdown document, R runs all code chunks from top to bottom and produces the output file.
Note that knitting uses a fresh R session. This means every object needed in the document must be created inside the document.
References
R for Data Science: https://r4ds.had.co.nz/r-markdown.html
R Cookbook: https://rc2e.com/rmarkdown
R Markdown Cookbook: https://bookdown.org/yihui/rmarkdown-cookbook/
The files for generating the notes and assignments (onQ -> R Markdown Examples)
You may use R Markdown for your project.
16.3 Basic formatting
Headings
Use # symbols to create headings.
# Level 1 Heading
## Level 2 Heading
### Level 3 Heading
#### Level 4 Heading
##### Level 5 Heading
###### Level 6 Heading
16.6 Lists
To create a bulleted list, start each line with *.
* first item
* second item
* third item
Result:
- first item
- second item
- third item
To create a numbered list, start each line with 1.
1. first item
1. second item
1. third item
Result:
- first item
- second item
- third item
You can also create nested lists.
1. first item
1. second item
a. subitem 1
a. subitem 2
i. sub-subitem 1
i. sub-subitem 2
a. subitem 2
1. third item
Result:
- first item
- second item
- subitem 1
- subitem 2
- sub-subitem 1
- sub-subitem 2
- subitem 2
- third item
16.7 Tables
You can create a simple table using pipes |.
| Decision | $H_0$ is true | $H_1$ is true |
|---|---|---|
| Reject $H_0$ | Type I error | No error |
| Do not reject $H_0$ | No error | Type II error |
Result:
| Decision | \(H_0\) is true | \(H_1\) is true |
|---|---|---|
| Reject \(H_0\) | Type I error | No error |
| Do not reject \(H_0\) | No error | Type II error |
16.8 R code in R Markdown
There are two main ways to include R code in R Markdown:
- inline R code
- code chunks
Code chunks
A code chunk is a block of R code enclosed by backticks.
Inline R code
Inline R code is used inside a sentence.
For example, the square root of x is 4.
The estimated coefficient of x is -0.5161.
16.9 Common chunk options
Chunk options control what is shown and what is run.
Show results but hide code
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -0.8356 -0.5462 0.2566 0.1322 0.5537 1.5953
Show code but do not run it
Run code but show neither code nor output
Hide warnings and messages
For more details, see:
16.11 LaTeX equations
R Markdown supports LaTeX-style math notation.
Inline math
Use a single pair of dollar signs for inline math.
Example: $x + 2$ gives \(x + 2\).
Display equations
Use a displayed equation when you want the math to appear on its own line.
\[\begin{equation*} x + 2 = 4 \end{equation*}\]
