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

OptionDescription
echoShow/hide code (TRUE/FALSE)
evalRun code (TRUE/FALSE)
includeInclude chunk in output (TRUE/FALSE)
results'asis', 'hide', 'hold', 'markup'
messageShow messages (TRUE/FALSE)
warningShow warnings (TRUE/FALSE)
errorContinue on error (TRUE/FALSE)
cacheCache results (TRUE/FALSE)
fig.widthFigure width in inches
fig.heightFigure height in inches
fig.capFigure caption
out.widthOutput width (e.g., "50%")

Related Cheat Sheets