Setting document title in Rmarkdown from parameters

Try to use a second YAML metadata block, and put the parameterized metadata in there.

I got the following code to work as expected (i.e., producing a document title from the list of params):

---
output: html_document
params: 
    set_title: "My Title!"
---

---
title: `r params$set_title`
---

The RMarkdown documentation notes that YAML metadata blocks are combined by Pandoc. Use the first block to define the parameter set, and the second one to use the parameters as metadata. Knitr will execute the R code to interpret the parameters in the second block.Then Pandoc will merge the metadata blocks together.

Update (2017):

This can be accomplished in a single block, like so:

---
output: html_document
params: 
    set_title: "My Title!"
title: "`r params$set_title`"
---

This works because the title comes after the params definition. I put quotes around the in-line R code to prevent “Scanner errors”.

Leave a Comment