R Markdown Exercises: 25 Practice Problems
Twenty-five practice problems on R Markdown: chunks, output formats, parameterised reports, tables, references, knitr options. Solutions hidden.
library(rmarkdown)
library(knitr)
Exercise 1: Minimal Rmd
Difficulty: Beginner. YAML + body that renders to HTML.
Show solution
```yaml---
title: "Demo"
output: html_document
---
# Hello
Some text.
```Exercise 2: PDF output
Difficulty: Intermediate.
Show solution
```yamloutput: pdf_document
```Exercise 3: Word output
Difficulty: Beginner.
Show solution
```yamloutput: word_document
```Exercise 4: Multiple outputs
Difficulty: Intermediate.
Show solution
```yamloutput:
html_document: default
pdf_document: default
```Exercise 5: Code chunk
Difficulty: Beginner.
mean(mtcars$mpg)
Show solution
A code chunk with {r} runs and shows code + output.
Exercise 6: Chunk options echo=FALSE
Difficulty: Intermediate.
plot(mtcars$wt, mtcars$mpg)
Show solution
Hides code; shows output. Useful for narrative-heavy reports.
Exercise 7: Chunk options message=FALSE
Difficulty: Intermediate.
library(dplyr)
Show solution
Suppresses package-load messages and warnings.
Exercise 8: Cache=TRUE
Difficulty: Intermediate.
slow_thing <- Sys.sleep(2)
Show solution
Saves chunk results so re-runs are fast.
Exercise 9: Inline R
Difficulty: Intermediate.
The mean MPG is `r mean(mtcars$mpg)`.
Show solution
Backtick-r-space inserts an R expression result inline.
Exercise 10: knitr::kable for tables
Difficulty: Intermediate.
knitr::kable(head(mtcars))
Show solution
Renders a basic table. For richer styling: kableExtra, gt, flextable.
Exercise 11: Figure caption
Difficulty: Intermediate.
plot(mtcars$wt, mtcars$mpg)
Show solution
fig.cap adds a caption below the rendered figure.
Exercise 12: Figure size
Difficulty: Intermediate.
plot(1:10)
Show solution
Inches by default. dpi controls resolution.
Exercise 13: Global options with knitr::opts_chunk
Difficulty: Advanced.
knitr::opts_chunk$set(echo = FALSE, warning = FALSE)
Show solution
Sets defaults for ALL chunks. Override per chunk as needed.
Exercise 14: Parameterised report
Difficulty: Advanced.
---
title: "Report"
output: html_document
params:
region: US
year: 2024
---
Show solution
Access via params$region. Render with rmarkdown::render(..., params = list(...)).
Exercise 15: TOC
Difficulty: Intermediate.
output:
html_document:
toc: true
toc_depth: 2
Show solution
Auto table-of-contents from headings.
Exercise 16: Theme
Difficulty: Intermediate.
output:
html_document:
theme: cosmo
Show solution
Built-in Bootswatch themes: cosmo, flatly, journal, etc.
Exercise 17: Code folding
Difficulty: Intermediate.
output:
html_document:
code_folding: hide
Show solution
User can show/hide code blocks. Options: none, show, hide.
Exercise 18: Tabsets
Difficulty: Advanced.
## Demo {.tabset}
### Tab A
content A
### Tab B
content B
Show solution
The {.tabset} class on a parent header turns child sections into tabs.
Exercise 19: Math equation
Difficulty: Beginner.
$$E = mc^2$$
Show solution
Double-dollar for display; single-dollar for inline. Renders via MathJax.
Exercise 20: Bibliography
Difficulty: Advanced.
output: html_document
bibliography: refs.bib
Show solution
Cite with [@key]. Generate refs section automatically.
Exercise 21: Render programmatically
Difficulty: Intermediate.
Show solution
rmarkdown::render("report.Rmd", output_format = "html_document")
Exercise 22: Custom output filename
Difficulty: Intermediate.
Show solution
rmarkdown::render("report.Rmd", output_file = "out_2024.html")
Exercise 23: Render with parameters
Difficulty: Advanced.
Show solution
rmarkdown::render("report.Rmd", params = list(region = "EU", year = 2024))
Exercise 24: Loop over many parameters
Difficulty: Advanced.
Show solution
for (r in c("US","EU","ASIA")) {
rmarkdown::render("report.Rmd",
output_file = paste0("report_", r, ".html"),
params = list(region = r))
}
Exercise 25: Quarto equivalent
Difficulty: Intermediate.
---
title: "Quarto"
format: html
---
Show solution
Quarto is the modern successor. quarto render report.qmd from terminal. Mostly compatible Rmd syntax.
What to do next
- Shiny-Exercises (shipped), interactive reports.
- ggplot2-Exercises (shipped), visuals inside reports.