Complete R Markdown syntax guide. RMarkdown, knitr, YAML front matter, code chunks, and output formats in one place.
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 |
Every R Markdown document starts with a YAML header that controls the title, author, date, and output format.
Minimum required front matter with title and output format
Common fields including author, date, and table of contents
Three most common output targets
Code chunks are the heart of R Markdown. They run R code and insert the output directly into the document.
Triple backticks with {r} to open a code chunk
Name and configure chunk behavior with key=value options
Most commonly used chunk options
Set defaults for all chunks at once with knitr::opts_chunk$set()
Embed computed R values directly in prose text using backtick-r syntax.
Wrap any R expression in backtick-r to embed its result in text
The mean is 3.
Reference variables defined in earlier chunks
Report generated on 2026-05-10.
The dataset has 150 rows.
R Markdown renders LaTeX math via MathJax in HTML and native LaTeX in PDF output.
Single dollar signs for inline LaTeX math
Einstein's formula is E = mc² where c is the speed of light.
Double dollar signs for centered block equations
R Markdown can produce HTML, PDF, Word, GitHub markdown, and presentation slides from a single source file.
Self-contained HTML with optional table of contents and themes
PDF via LaTeX — requires a LaTeX distribution (TinyTeX recommended)
Microsoft Word .docx output, accepts a reference_docx for styling
Renders to GitHub-flavored markdown — ideal for READMEs with embedded plots
PDF slide deck using LaTeX Beamer — for academic presentations
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
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.
Everything you need to know.
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.
Convert any markdown document to a polished PDF, HTML page, or Word doc instantly.