Reports with R Markdown
R Markdown is a document format that mixes Markdown prose with runnable R code chunks, so that a single "knit" produces a reproducible report — HTML, PDF, or Word — where every number, table, and plot is generated straight from your data.
Learn Reports with R Markdown in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll write the YAML header, add code chunks and inline R, control output with chunk options, and knit a polished, reproducible report.
What You'll Learn in This Lesson
1️⃣ The YAML Header and Markdown Body
Every .Rmd file opens with a YAML header between two --- lines that sets the title, author, and most importantly the output format. Below it you write ordinary Markdown — headings with # , bold with **...** , and bullet lists.
2️⃣ Code Chunks and Inline R
A code chunk opens with a fenced header — three backticks then {' '} — and closes with three backticks. Knitr runs the code and inserts the result. For a single value inside a sentence, use inline R: a backtick, the letter r , your expression, then a closing backtick.
3️⃣ Chunk Options and Knitting
Each chunk header can carry options that shape what appears: echo=FALSE hides the code, eval=FALSE skips running it, and fig.width / fig.height size plots. Set document-wide defaults in a setup chunk, then knit to HTML, PDF, or Word.
Your turn. Fill in the TODO blanks, save as report.Rmd , and knit it.
Assemble a complete mini-report from the spec: YAML, a setup chunk, a data frame, an inline total, and a captioned plot. Then change one number and re-knit to feel the power of reproducibility.
📋 Quick Reference — R Markdown
Practice quiz
What lives in the YAML header between the two --- lines?
- The R code chunks
- Document metadata like title, author, and output format
- The bibliography
- Only the page title
Answer: Document metadata like title, author, and output format. The YAML header sets title, author, date, and the output format.
How do you open an R code chunk in R Markdown?
- <r>
- Three backticks followed by {r}
- #{r}
Answer: Three backticks followed by {r}. A chunk opens with three backticks then {r} and closes with three backticks.
What does the chunk option echo=FALSE do?
- Runs the code but hides the source, showing only output
- Skips running the chunk
- Hides both code and output
- Adds a caption
Answer: Runs the code but hides the source, showing only output. echo=FALSE hides the source code while still showing the result.
What does eval=FALSE do to a chunk?
- Hides the output
- Hides the code
- Shows the code but does not run it
- Caches the chunk
Answer: Shows the code but does not run it. eval=FALSE displays the code without executing it.
What does include=FALSE do?
- Shows code but not output
- Shows output but not code
- Adds the chunk to the table of contents
- Runs the chunk but shows neither code nor output
Answer: Runs the chunk but shows neither code nor output. include=FALSE runs the chunk for side effects but hides code and output.
How do you embed a single computed value inside a sentence?
- Inline R: a backtick, the letter r, an expression, a backtick
- A {r} block
- Using <<r>> tags
- With paste() only
Answer: Inline R: a backtick, the letter r, an expression, a backtick. Inline R drops one value into prose with .
Which function sets document-wide chunk defaults?
- rmarkdown::render()
- knitr::opts_chunk$set()
- yaml::set()
- options(chunk = ...)
Answer: knitr::opts_chunk$set(). knitr::opts_chunk$set() in a setup chunk sets global defaults.
How do you produce a PDF instead of HTML?
- Rename the file to .pdf
- Use eval=FALSE
- Change output: to pdf_document in the YAML
- Call print()
Answer: Change output: to pdf_document in the YAML. Set output: pdf_document in the YAML header (a LaTeX engine is needed).
Which function renders an .Rmd report from code?
- rmarkdown::render("report.Rmd")
- knit_now()
- source("report.Rmd")
- render.csv()
Answer: rmarkdown::render("report.Rmd"). rmarkdown::render() knits the document without the RStudio button.
Why prefer R Markdown over a script with pasted results?
- It runs faster
- It uses less memory
- It needs no packages
- It is reproducible: numbers regenerate from data each knit
Answer: It is reproducible: numbers regenerate from data each knit. Code, output, and prose live in one file, so re-knitting updates every figure.