RMarkdown list in YAML

Is there a way to insert a list into YAML as R code.

The following works in the YAML header

title: "My Title"
date: "`r Sys.Date()`"

Then in the pandoc template these can be accessed using $title$ and $date$

The following would also work:

title: "My Title"
dates:
  today_date: "`r Sys.Date()`"
  tomorrow_date: "`r Sys.Date() + 1`"

And they can be accessed using $dates.today_date$ and $dates.tomorrow_date$

However, when I use an R function that returns a list of value, this does not work:

title: "My Title"
dates: "`r list(today_date = Sys.Date(), tomorrow_date= Sys.Date() + 1)`"

I would expect $dates.today_date$ to still evaluate the same, however it is returning blank. I have tried wrapping in yaml::as.yaml() which also didn't work. Is there a way to access r lists inside the YAML?

The syntax

`r code`

is knitr inline code. The codepart must evaluate to a string (or can be convert to) to be inserted into the markdown document.

Inserting a list that way won't work as you expect.

With YAML you can insert some arbitraty R object using this syntax

key: !expr list(a = 1, b = 2)

This would parse for YAML in R as a list in the key value.

This can be useful for example to use a custom fonction in df_print (see https://bookdown.org/yihui/rmarkdown/html-document.html#custom-fun-dfprint)

This is a feature of the yaml package. See ?yaml::yaml.load

This will work and be useful for yaml fields that are processed by R. Some fields are used as Pandoc variable.
More of

However, in your case, usually the date fields is used in the pandoc template and should be a string to be used. if you set as a list it won't be correctly pick up by Pandoc. https://pandoc.org/MANUAL.html#variables

Hope it helps

I marked this as a solution without actually trying it out. I only used date as an example. I've tried this now, but it doesn't seem to be being parsed correctly.

In the YAML:

test: !expr list(a=2+2, b = 3)

In the template.tex:

test:
$test$

test a:
$test.a$

test b:
$test.b$

In the output.tex:

test:
list(a=2+2, b = 3)

test a:


test b:

So the expression isn't being evaluated at all. If I run rmarkdown::yaml_front_matter() on the file, it gets evaluated correctly. I also made sure I set options(yaml.eval.expr = TRUE)

What I meant by this is that it will only work for fields in the YAML that are processed by R, and not those used in a Pandoc template, parsed by Pandoc from the YAML into metadata and variables.

For those, I don't think there is an easy way to pass them to Pandoc using the YAML header. Usually, inline R chunk can be used for that when you need only a string but here you want to create nested fields from R to pass as pandoc variable. That is why you manage to do it using

title: "My Title"
dates:
  today_date: "`r Sys.Date()`"
  tomorrow_date: "`r Sys.Date() + 1`"

However, this could be done using some advanced Pandoc features like multiple yaml blocks or passing a metadata file are understand by Pandoc. You could make so that knitr will output the correct part or file for Pandoc to understand when converting from md to your output format.
This is to use with caution, and you must know deeply how Pandoc works because but it could be tricky to debug and could have side effects with R Markdown features. But I mention this because it is possible.
Example below - see the output md document document.

---
title: "settings Pandoc variables from R Markdown"
output: 
  md_document:
    pandoc_args: 
     - "--metadata-file"
     - "meta.yml"
     - "--template"
     - "template.md"
---


Adding a new block programatically would work

---
```{r, results='asis', echo = FALSE}
cat(yaml::as.yaml(list(test = list(a = 2+2, b = 3))))
```
---

Adding a metadata file also
```{r}
yaml::write_yaml(
  list(test2 = list(c = 3+3, d = 5)),
  'meta.yml')
```

Creating the template for the reprex

```{cat, engine.opts=list(file = 'template.md')}
test:
$test$

test a:
$test.a$

test b:
$test.b$

test2:
$test2$

test2 c:
$test2.c$

test2 d:
$test2.d$
```

1 Like

This solution worked perfectly. I've now managed to add in the extra parameters that I need into my file. Thanks :smiley:

1 Like

Glad you found it useful ?

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.