R Markdown Guide
Complete R Markdown syntax guide. RMarkdown, knitr, YAML front matter, code chunks, and output formats in one place.
Quick Reference
All R Markdown syntax at a glance.
| Feature | Syntax | Notes | |
|---|---|---|---|
| YAML front matter | ---
title: "My Report"
output: html_document
--- | Must be at the very top of the file | |
| Code chunk | ```{r chunk-name, echo=TRUE}
code here
``` | Named chunks are easier to debug | |
| Inline R code | `r mean(x)` | Embed computed values in prose | |
| Heading H1 | # Heading | Standard markdown headings work | |
| Heading H2 | ## Heading | H1-H6 all supported | |
| Heading H3 | ### Heading | ||
| Bold | **bold** | Double asterisks | |
| Italic | *italic* | Single asterisks | |
| Bullet list | - item | Dash, asterisk, or plus | |
| Numbered list | 1. item | Number + period | |
| Table | | Col | Col | | Standard markdown tables | |
| Image |  | Local or remote image path | |
| Math inline | $E = mc^2$ | LaTeX between dollar signs | |
| Math block | $$\sum_{i=1}^{n} x_i$$ | Double dollar signs for display math | |
| Link | [text](url) | Standard markdown link syntax |
YAML Front Matter
Every R Markdown document starts with a YAML header that controls the title, author, date, and output format.
Basic YAML Header
Minimum required front matter with title and output format
Full YAML Header
Common fields including author, date, and table of contents
Output Formats
Three most common output targets
Code Chunks
Code chunks are the heart of R Markdown. They run R code and insert the output directly into the document.
Basic Code Chunk
Triple backticks with {r} to open a code chunk
Named Chunk with Options
Name and configure chunk behavior with key=value options
Chunk Options Reference
Most commonly used chunk options
Global Options Setup Chunk
Set defaults for all chunks at once with knitr::opts_chunk$set()
Inline R Code
Embed computed R values directly in prose text using backtick-r syntax.
Inline R Expression
Wrap any R expression in backtick-r to embed its result in text
The mean is 3.
Inline Date and Variable
Reference variables defined in earlier chunks
Report generated on 2026-05-10.
The dataset has 150 rows.
Math & Equations
R Markdown renders LaTeX math via MathJax in HTML and native LaTeX in PDF output.
Inline Math
Single dollar signs for inline LaTeX math
Einstein's formula is E = mc² where c is the speed of light.
Display Math Block
Double dollar signs for centered block equations
Output Formats
R Markdown can produce HTML, PDF, Word, GitHub markdown, and presentation slides from a single source file.
HTML Document
Self-contained HTML with optional table of contents and themes
PDF Document
PDF via LaTeX — requires a LaTeX distribution (TinyTeX recommended)
Word Document
Microsoft Word .docx output, accepts a reference_docx for styling
GitHub Document
Renders to GitHub-flavored markdown — ideal for READMEs with embedded plots
Beamer Presentation
PDF slide deck using LaTeX Beamer — for academic presentations
Unsupported in R Markdown
Features that require workarounds or are platform-specific.
No CSS without raw HTML blocks — use output-specific styling instead
Use Shiny for interactive R applications
Platform-specific markdown (spoilers, mentions) is not supported
Pro Tips
Give every code chunk a meaningful name (e.g., ```{r load-data}). Named chunks appear in the RStudio navigator and produce clearer error messages when something goes wrong.
```{r load-data, echo=FALSE}
data <- read.csv("data.csv")
```Set echo=FALSE on chunks where you only want to show the output (like a plot or table), not the code itself. Great for polished reports.
```{r plot-results, echo=FALSE}
plot(data$x, data$y)
```Put a setup chunk at the very top of your document to set default chunk options for the entire file. This avoids repeating options on every chunk.
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE
)
```RStudio has built-in R Markdown support. Click the Knit button (or press Ctrl+Shift+K / Cmd+Shift+K) to render your document. The output opens automatically in the Viewer pane.
Frequently asked questions
Everything you need to know.
What is R Markdown? How does it differ from regular markdown?
R Markdown is an extension of standard markdown that lets you embed executable R code directly in your document using special "code chunks" (```{r} ... ```). When you render (knit) the document, knitr runs the code and replaces each chunk with its output — tables, plots, values. Regular markdown is static text; R Markdown is a reproducible research document that generates output on demand.
Export your R Markdown output?
Convert any markdown document to a polished PDF, HTML page, or Word doc instantly.