60

I've got an Rmarkdown template that works well, and I've parameterized it so I can generate variants of the same report from different data sources. However, I'd like to change the title of the report in each case. How do I do that?

Here's the YAML header I have so far:

---
title: "My Title"
author: "Me, Inc."
date: "August 4, 2015"
output: pdf_document
params:
  title: default
---

I've tried using params=list(title="ASDF") in the call to rmarkdown::render, and although my code can see that variable, it doesn't change the title. I've also tried using r params$title in the YAML, but that gives a syntax error.

Is there something else I should be trying? Thanks!

Harlan
  • 18,883
  • 8
  • 47
  • 56

3 Answers3

102

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".

TJ Mahr
  • 3,846
  • 1
  • 21
  • 22
  • 15
    You can actually do it with a single YAML block as long as `title: ...` comes *after* the `params` declaration. – eipi10 Mar 16 '16 at 21:24
  • 5
    If you get a `Scanner error` you may need quote marks either side the backticks (i.e. make the value a string for the YAML parser). – blmoore Oct 20 '16 at 17:51
  • 10
    In this example "My Title!" is a text string. How would you change this if you wanted to the title to be created dynamically based on a variable in the data (which can change depending on the source of that data)? – Lloyd Christmas Mar 28 '17 at 20:59
  • I'm not sure if it's my setup, but when I used a logical for `set_title`, then I had to use a second YAML metablock as in the original answer above – chandler Sep 02 '17 at 18:02
  • 3
    **This also works AFTER you run some code, like importing and transforming data**, using the first option of 2 YAML blocks @Lloyd Christmas – MS Berends Oct 12 '17 at 09:15
9

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!

Eric Leung
  • 2,354
  • 12
  • 25
Lahi S
  • 131
  • 2
  • 7
3

Adding this answer as it helps in making R markdown titles dynamic.

Just use !r followed by the object name defined (test_title in the case below) to make the title dynamic.

---
output: pdf_document
params:
set_title: !r test_title
---
---
title: `r params$set_title`
---
Rishabh
  • 81
  • 2
  • 8