R Markdown
Reproducible Reports
Learn how to combine R code, narrative text, and output in a single document that anyone can reproduce and verify.
What Is R Markdown?
R Markdown is an authoring framework that lets you write a single .Rmd file containing prose, code, and results. When you render ("knit") the file, R executes every code chunk and weaves the output into a polished document.
Why Reproducibility Matters
Traditional workflows separate analysis (code) from reporting (Word, slides). When data changes, you manually copy-paste updated numbers: a recipe for errors and inconsistency. R Markdown solves this by keeping everything in one place.
The Three Layers of an .Rmd File
The YAML Header
YAML ("Yet Another Markup Language") sits between triple dashes --- at the very top of your file. It controls metadata and output settings.
Markdown Syntax Essentials
Between code chunks, you write in Markdown: a lightweight formatting language. Here are the most common elements:
| Syntax | Result |
|---|---|
| # Heading 1 | Top-level heading |
| ## Heading 2 | Section heading |
| **bold text** | bold text |
| *italic text* | italic text |
| - item | Bullet list item |
| [text](url) | Hyperlink |
|  | Image embed |
Code Chunks: The Engine
Code chunks are fenced with triple backticks and a language specifier. They are the executable heart of your document.
Key Chunk Options
.R Script vs .Rmd File
| .R Script | .Rmd File | |
|---|---|---|
| Purpose | Pure code execution | Code + narrative report |
| Text support | Comments only (#) | Full Markdown formatting |
| Output | Console/plots only | HTML, PDF, Word, slides |
| Reproducibility | Requires separate docs | Self-contained document |
| Sharing | Send code + report separately | One file produces everything |
The Knit Pipeline
Creating Your First Report
Inline Code: Dynamic Values
Beyond code chunks, you can embed single computed values directly into your narrative text using inline R code. This ensures numbers in your prose always match your data.
Example 1: Basic Data Summary
Example 2: Embedding a Plot
Example 3: Formatted Tables
Common Mistakes
Summary: R Markdown at a Glance
| Concept | What It Does | Key Syntax |
|---|---|---|
| YAML Header | Sets metadata and output format | --- title: "..." --- |
| Markdown | Formats narrative text | # heading, **bold** |
| Code Chunks | Executes R code | ```{r} ... ``` |
| Inline Code | Embeds computed values | `r expression` |
| Chunk Options | Controls chunk behavior | echo, eval, fig.width |
| Knit | Renders final document | Ctrl+Shift+K |
Question 1 of 5
What are the three main layers of an R Markdown (.Rmd) file?
Question 2 of 5
What does the chunk option echo = FALSE do?
Question 3 of 5
Which tool in the knitting pipeline is responsible for converting .md into HTML or PDF?
Question 4 of 5
How do you embed a computed value directly inside a sentence in R Markdown?
Question 5 of 5
Which of the following is NOT a valid output format specified in the YAML header?