Setting document title in Rmarkdown from parameters
This is a more simplified approach to the dynamic title challenge.
Decouple title from the top declaration like this:
From this:
---
title: "Sample Title"
output: pdf_document
---
To This:
---
output: pdf_document
---
```{r}
title_var <- "Sample Title"
```
---
title: `r title_var`
---
Within the R code chunks, declare title_var
. Now the title is held within a variable. Hope this helps!
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".