Setting Document Title in Rmarkdown from Parameters

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

Parameterize both Author and Title in Markdown using a loop

I almost always avoid for loops, since one of the beauties of R is that you can work over vectors. Instead, I'd use an apply family function from base R or, my preference, a map family function from purrr/tidyverse.

You can do this a few ways, but I went with a nested list. It's a list of course info, where each course is a list of the professor's name and the class name. Using walk, you map over the outer list, take the names from each class, and use those as parameters for render.

Here's the dummy Rmarkdown, with the filename dummy_rmd.Rmd:

Edit: You can use inline R code inside your yaml to set the title and author of the document, as explained in this answer. The items with the inline code need to be after params, so there's something defined already.

---
output: html_document
params:
prof: "Person 1"
class: "Class A"
title: "`r params$class`"
author: "`r params$prof`"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
prof <- params$prof
class <- params$class

plot(1:5, runif(5), main = sprintf("Plot for %s taught by %s", class, prof))
```

Then in a script in the same directory:

class_list <- list(
list(prof = "Person 1", class = "Class A"),
list(prof = "Person 2", class = "Class B"),
list(prof = "Person 3", class = "Class C")
)

purrr::walk(class_list, function(class_info) {
prof <- class_info$prof
class <- class_info$class
rmarkdown::render(
input = "dummy_rmd.Rmd",
output_file = sprintf("output_%s_%s.html", prof, class),
params = list(prof = prof, class = class)
)
})

This gives me html files, one for each course, named accordingly. HTML output looks like:

Sample Image

how to display user defined title in r markdown?

Try this in the title field of your header. I believe that you can do this to output any r variable from a code chunk as text, even in a header.

---
title: `r title`
author: "your_name"
date: "11/18/2016"
output: pdf_document
---

R markdown: Accessing variable from code chunk (variable scope)

Why Rmarkdown Dynamic TITLE in YAML does not appear in the pdf file

For closing the conversation of this question I write how has been solved this question.
At the beginning I was thinking this issue was related with the code in YAML (where the title was informed from parameters) but eventually I discovered that there was nothing to do that. The problem was that I was using a the function cat() in the varible title that I pass as a parametre and this function changes the value of variable title into NULL.

For knowing exactly all the explanation about how to write dynamic titles in rmarkdown documents redirect yourself to the next entrance: Setting document title in Rmarkdown from parameters

Passing multiple parameters in RMarkdown document

RMD file -

---
title: "`r params$MVNDR_NBR`"
author: "Santiago Canon"
date: "5/26/2021"
output:
html_document:
highlight: monochrome
theme: flatly
params:
MVNDR_NBR: NA
MVNDR_NM: NA

---


<font size="4"> This document will provide a summary of "`r params$MVNDR_NM`" performance within in QC: </font>

for loop -

mvndr_nm <- c('name1', 'name2', 'name3', 'name4', 'name5', 'name6')
mvndr_nbr<- c('60031167', '60688509', '60074051', '60148060', '60086898', '60080204')


for (i in seq_along(mvndr_nm)) {
rmarkdown::render("C:/Users/santi/Documents/R Scripts/Export_Data_CSV.Rmd",
output_file = sprintf("MVNDR_%s.html", mvndr_nbr[i]),
params = list(MVNDR_NBR = mvndr_nbr[i], MVNDR_NM = mvndr_nm[i]))
}

Output -

MVNDR_60031167.html

Sample Image

MVNDR_60688509.html -

Sample Image



Related Topics



Leave a reply



Submit