Jan 28, 2026

Some - semi-advanced R Markdown

YAML

What is YAML?

YAML Ain’t Markup Language

  • Header block at top of .Rmd
  • Enclosed by ---
  • Controls document metadata & output
  • Key-value pairs with colons
---
title: My Document
author: Me
output: html_document
---

Pieces of a YAML

Basic metadata

---
title: "My Analysis"
subtitle: "A deeper dive"
author: "Dr. Gurarie"
date: "January 28, 2026"
output: html_document
---

Key rules

  • Indentation matters (2 spaces)
  • Quotes optional for simple strings
  • Nested options indented below parent

Output Types

Documents

output: html_document
output: pdf_document
output: word_document

Presentations

output: ioslides_presentation
output: slidy_presentation
output: beamer_presentation

Note: pdf_document and beamer_presentation require LaTeX

Table of Contents

output:
  html_document:
    toc: true
    toc_depth: 3
    toc_float: true

Options

  • toc: true — enable TOC
  • toc_depth: n — heading levels
  • toc_float: true — sticky sidebar (html only)

Nested Output Options

output:
  html_document:
    toc: true
    toc_float: true
    theme: flatly
    code_folding: hide
    fig_width: 8
    fig_height: 6

Each output type has its own set of sub-options — check the documentation!

Using R Code in YAML

Dynamic date

date: "`r Sys.Date()`"

outputs:

date: "2026-01-26"

Or Dynamic anything!

author: "`r paste(LETTERS[c(8,25,1,20,20)], collapse = "")`"
date: "`r Sys.Date()`"

outputs:

author: "HYATT" 
date: "2026-01-26"

Fancy Themes

install.packages("rmdformats")

output: 
  rmdformats::readthedown
output: 
  rmdformats::material
output:
  rmdformats::downcute

Check out gallery: github.com/juba/rmdformats

Prettydoc

Presentation Packages

Package Output Notes
ioslides HTML slides Built-in, simple
xaringan HTML slides Highly customizable, remark.js
revealjs HTML slides 3D transitions
beamer PDF slides LaTeX-based

YAML for xaringan

For example: Lecture: How to Model Anything

---
title: "How to model just about anything<br>(but especially habitat) Part II"
subtitle: "EFB 390: Wildlife Ecology and Management"
author: "Dr. Elie Gurarie"
date: "October 3, 2024"
output: 
  xaringan::moon_reader:
    css: [default, default-fonts, mycss.css]
    nature:
      highlightStyle: github
      countIncrementalSlides: false
      highlightLines: true
      titleSlideClass: ["center","middle","white"]
      ratio: '16:9'
editor_options: 
  chunk_output_type: console
---

Chunks

What is a Chunk?

Input:

```{r}
mean(c(1, 2, 3))
```

Output:

mean(c(1, 2, 3))
## [1] 2

Naming Chunks

```{r my-summary-stats}
summary(iris)
```

Why name chunks?

  • Easier debugging (error messages reference name)
  • Required for cached chunks & cross-references
  • Navigation in RStudio outline pane

Rules: no spaces, no underscores (use -)

eval: Run the Code?

Input:

```{r eval=FALSE}
2 + 2
```

```{r eval=2}
2 + 2
cat("Dr. Green Teach Good")
```

Output:

2 + 2

(code shown, not executed)

## 2 + 2
cat("Dr. Green Teach Good")
## Dr. Green Teach Good

only line 2 evaluated

echo: Show the Code?

Input:

```{r echo=FALSE}
2 + 2
```

```{r echo=-2}
2 + 2
cat("Dr. Green Teach Good")
```

Output:

## [1] 4

output only, code hidden

2 + 2
## [1] 4
## Dr. Green Teach Good

don’t show the second line of code - just the output

eval + echo Combinations

eval echo Result
TRUE TRUE Code + Output
TRUE FALSE Output only
FALSE TRUE Code only (display)
FALSE FALSE Nothing!

Figure Options: Default

Input:

```{r}
k <- 10
x <- seq(-1,1,length = 1000)
M <- sapply(k:1, function(f) 
  cos(x*f * pi) * sqrt(1 - x^2))
matplot(x, M, type = "l", col = 1:k,
        lty = 1, lwd = seq(1,3,length = k))
```

Output:

Figure Options: Size

Input:

```{r fig.width=4, fig.height=3}
k <- 10
x <- seq(-1,1,length = 1000)
M <- sapply(k:1, function(f) 
  cos(x*f * pi) * sqrt(1 - x^2))
matplot(x, M, type = "l", col = 1:k,
        lty = 1, lwd = seq(1,3,length = k))
```

Output:

Figure Options: out.width

Input:

```{r out.width="50%"}
k <- 10
x <- seq(-1,1,length = 1000)
M <- sapply(k:1, function(f) 
  cos(x*f * pi) * sqrt(1 - x^2))
matplot(x, M, type = "l", col = 1:k,
        lty = 1, lwd = seq(1,3,length = k))
```

out.width scales the displayed size

fig.width sets the rendered size

Output:

cache: Save Time on Long Computations

``` r
model <- lm(mpg ~ ., data = mtcars)
```
  • Saves chunk output to disk
    • (including figures! Can be very useful.)
  • Skips re-running if code is unchanged
  • Creates _cache/ folder

Caution: Won’t detect changes in upstream dependencies.

The Setup Chunk

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
  fig.width = 8,
  fig.height = 5
)
```
  • First chunk in document
  • include = FALSE hides it entirely
  • Sets defaults for ALL subsequent chunks
  • Individual chunks can override

Other Useful Options

Option Purpose
message = FALSE Hide messages
warning = FALSE Hide warnings
include = FALSE Run but hide everything
results = 'hide' Hide text output only
error = TRUE Keep knitting despite errors
comment = "" Remove ## from output