Skip to content
Free · No signup · Instant

R Markdown Guide

Complete R Markdown syntax guide. RMarkdown, knitr, YAML front matter, code chunks, and output formats in one place.

YAML front matterCode chunksMultiple outputsFree forever

Quick Reference

All R Markdown syntax at a glance.

FeatureSyntax
YAML front matter--- title: "My Report" output: html_document ---
Code chunk```{r chunk-name, echo=TRUE} code here ```
Inline R code`r mean(x)`
Heading H1# Heading
Heading H2## Heading
Heading H3### Heading
Bold**bold**
Italic*italic*
Bullet list- item
Numbered list1. item
Table| Col | Col |
Image![alt](path)
Math inline$E = mc^2$
Math block$$\sum_{i=1}^{n} x_i$$
Link[text](url)

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

README.md
---\ntitle: "My Report"\noutput: html_document\n---

Full YAML Header

Common fields including author, date, and table of contents

README.md
---\ntitle: "Quarterly Report"\nauthor: "Jane Smith"\ndate: "`r Sys.Date()`"\noutput:\n html_document:\n toc: true\n toc_float: true\n---

Output Formats

Three most common output targets

README.md
output: html_document\n# or\noutput: pdf_document\n# or\noutput: word_document

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

README.md
```{r}\nsummary(mtcars)\n```

Named Chunk with Options

Name and configure chunk behavior with key=value options

README.md
```{r load-data, echo=FALSE, message=FALSE}\nlibrary(tidyverse)\ndata <- read_csv("data.csv")\n```

Chunk Options Reference

Most commonly used chunk options

README.md
echo = TRUE # show source code?\neval = TRUE # run the code?\ninclude = TRUE # include output?\nwarning = TRUE # show warnings?\nmessage = TRUE # show messages?\nfig.width = 7 # plot width (inches)\nfig.height = 5 # plot height (inches)

Global Options Setup Chunk

Set defaults for all chunks at once with knitr::opts_chunk$set()

README.md
```{r setup, include=FALSE}\nknitr::opts_chunk$set(\n echo = TRUE,\n warning = FALSE,\n message = FALSE,\n fig.width = 8,\n fig.height = 5\n)\n```

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

README.md

The mean is 3.

Inline Date and Variable

Reference variables defined in earlier chunks

README.md

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

README.md

Einstein's formula is E = mc² where c is the speed of light.

Display Math Block

Double dollar signs for centered block equations

README.md
i=1n xi = x₁ + x₂ + ⋯ + xn
x̄ = (1/n) ∑i=1n xi

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

README.md
output:\n html_document:\n toc: true\n toc_float: true\n theme: flatly\n highlight: tango

PDF Document

PDF via LaTeX — requires a LaTeX distribution (TinyTeX recommended)

README.md
output:\n pdf_document:\n toc: true\n number_sections: true\n latex_engine: xelatex

Word Document

Microsoft Word .docx output, accepts a reference_docx for styling

README.md
output:\n word_document:\n reference_docx: my-template.docx

GitHub Document

Renders to GitHub-flavored markdown — ideal for READMEs with embedded plots

README.md
output: github_document

Beamer Presentation

PDF slide deck using LaTeX Beamer — for academic presentations

README.md
output:\n beamer_presentation:\n theme: "AnnArbor"\n colortheme: "dolphin"

Unsupported in R Markdown

Features that require workarounds or are platform-specific.

Inline styles

No CSS without raw HTML blocks — use output-specific styling instead

Interactive elements

Use Shiny for interactive R applications

Discord/Slack-specific syntax

Platform-specific markdown (spoilers, mentions) is not supported

Pro Tips

1Always name your chunks

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")
```
2Use echo=FALSE to hide code

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)
```
3Set global options at the top

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
)
```
4RStudio Knit button

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.

1

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.