Using rmarkdown parameters to set title, author, and date

Another quick tip today. I’m taking a look at a bunch of my Rmarkdown documents for places I can employ parameters.

For reports and other templates, you might want to parameterize the YAML header that defines the title, author, date, etc. Turns out this is more straightforward than I guessed. You just need to define the parameters near the top of the YAML, then assign them to the appropriate elements later on.

Here’s an example header for a Xaringan slide deck:

---
output:
  xaringan::moon_reader:
    lib_dir: libs
    css: xaringan-themer.css
    nature:
      ratio: '16:10'
      highlightStyle: solarized-light
      highlightLines: true
      countIncrementalSlides: false
params:
  title: ""
  subtitle: ""
  author: "Jason Winget"
  date: "`r Sys.Date()`"

title: "`r params$title`"
subtitle: "`r params$subtitle`"
author: "`r params$author`"
date: "`r params$date`"
---

When you use knit_with_parameters() to compile this document, it will pop up a shiny window where you can enter the values for this report.

Even if you don’t change these too much, using parameters like this can be useful for a common template file or automating report generation.

Be awesome today!