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?
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
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
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$
```
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: