R Markdown Cheat Sheet
Complete guide to R Markdown for reproducible research and data science
R Markdown extends standard Markdown with R code chunks. See our Markdown Cheat Sheet for basic syntax.
YAML Front Matter
Basic Document
--- title: "My Document" author: "Your Name" date: "2024-01-01" output: html_document ---
PDF Output
--- title: "PDF Report" output: pdf_document ---
Multiple Outputs
---
title: "Multi-format"
output:
html_document:
toc: true
pdf_document: default
---Code Chunks
Basic R Chunk
```{r}
summary(cars)
```Named Chunk
```{r my-chunk-name}
x <- 1:10
mean(x)
```Python Chunk
```{python}
import pandas as pd
print("Hello from Python")
```Chunk Options
Hide Code
```{r, echo=FALSE}
plot(1:10)
```Hide Output
```{r, results='hide'}
x <- 1:10
```Don't Evaluate
```{r, eval=FALSE}
# This code won't run
install.packages("ggplot2")
```Figure Size
```{r, fig.width=10, fig.height=6}
plot(cars)
```Figure Caption
```{r, fig.cap="My Plot Title"}
plot(cars)
```Cache Results
```{r, cache=TRUE}
# Slow computation
result <- complex_function()
```Inline R Code
Basic Inline
The mean is `r mean(x)`.
With Formatting
The value is `r round(pi, 2)`.
Tables
kable Table
```{r}
knitr::kable(head(iris))
```Styled Table
```{r}
knitr::kable(head(mtcars), caption = "Motor Trend Cars")
```Common Chunk Options
| Option | Description |
|---|---|
echo | Show/hide code (TRUE/FALSE) |
eval | Run code (TRUE/FALSE) |
include | Include chunk in output (TRUE/FALSE) |
results | 'asis', 'hide', 'hold', 'markup' |
message | Show messages (TRUE/FALSE) |
warning | Show warnings (TRUE/FALSE) |
error | Continue on error (TRUE/FALSE) |
cache | Cache results (TRUE/FALSE) |
fig.width | Figure width in inches |
fig.height | Figure height in inches |
fig.cap | Figure caption |
out.width | Output width (e.g., "50%") |