7

In short: Is there a way to manipulate RMarkdown's metadata-list generated from the YAML header from within the following R-code chunks? To illustrate, I've tried the following:

---
title: "Untitled"
output: html_document
---

```{r}
rmarkdown::metadata$title <- "New title"
rmarkdown::metadata$title
```

This throws an error, though.

Error in rmarkdown::metadata$title <- "New title" : 
  Object 'rmarkdown' not found

Background

I'm working on a RMarkdown TeX-template. Some parts of the preamble need to be localized depending on the variable lang defined in the YAML header. My current approach is to check the value of metadata$lang and create a list of corresponding terms. I wanted to add the contents of the list to metadata and access the terms in the TeX-template via $loc_wordcount$, for example.

crsh
  • 1,699
  • 16
  • 33

1 Answers1

0

I think that the answer to this is no (though, I'm not 100% sure of that).

But, there is a different way to do it. You can use a parameterized report (https://bookdown.org/yihui/rmarkdown-cookbook/parameterized-reports.html).

The way to do that would be:

Create an rmarkdown file (e.g. called "input.Rmd") with parameters named param$value:

---
title: "params$new_title"
output: html_document
---

The rest of the report goes here.

Then render the report with a parameter passed in:

rmarkdown::render('input.Rmd', params = list(new_title = "New title"))
Bill Denney
  • 766
  • 1
  • 6
  • 21