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

  1. R for Data Science: https://r4ds.had.co.nz/r-markdown.html

  2. R Cookbook: https://rc2e.com/rmarkdown

  3. R Markdown Cookbook: https://bookdown.org/yihui/rmarkdown-cookbook/

  4. 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.4 This is a subsection

16.4.1 This is a subsubsection

16.5 Formatting Text

  • *italics* gives italics
  • **bold** gives bold
  • `code` gives code

See also: https://rc2e.com/rmarkdown#tab:commonMarkdown

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:

  1. first item
  2. second item
  3. 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:

  1. first item
  2. second item
    1. subitem 1
    2. subitem 2
      1. sub-subitem 1
      2. sub-subitem 2
    3. subitem 2
  3. 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.

x <- 16
sqrt(x)
## [1] 4

sqrt(x)
## [1] 4

sqrt(x)
## [1] 4

Inline R code

Inline R code is used inside a sentence.

For example, the square root of x is 4.

set.seed(1)
x <- rnorm(10)
y <- rnorm(10)
fit <- lm(y ~ x)

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

summary(x)

Run code but show neither code nor output

Hide warnings and messages

library(tidyverse)

For more details, see:

https://rc2e.com/rmarkdown#recipe-code-results

16.10 Graphics

R Markdown can display graphs created by R.

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*}\]

Reference: https://en.wikibooks.org/wiki/LaTeX/Mathematics

16.12 Common mistakes

Here are a few common mistakes when using R Markdown:

  • forgetting to close a code chunk with three backticks

  • using an object that was created in the Console but not in the document

  • forgetting the r in inline R code

  • trying to knit to PDF before LaTeX is installed