What is the advised way to evaluate R expression in yaml headers retrieved with rmarkdown:::yaml_front_matter

Hello, I am using rmarkdown:::yaml_front_matter to retrieve parameters from a Rmd documents.
However when I use R code in those params, those expression are not evaluated

---                                  
title: "Test"                        
author: "me"                         
output: html_document                
params:                              
 start_date: !r as.Date('2022-01-01')
 end_date: as.Date('2022-01-31')     
 css: css/styles.css                 
 warning: FALSE                      
 out.width: "100%"                   
---                                  
R> rmarkdown::yaml_front_matter("/tmp/test.Rmd")
$title                                          
[1] "Test"                                      

$author                                         
[1] "me"                                        

$output                                         
[1] "html_document"                             

$params                                         
$params$start_date                              
[1] "as.Date('2022-01-01')"                     

$params$end_date                                
[1] "as.Date('2022-01-31')"                     

$params$css                                     
[1] "css/styles.css"                            

$params$warning                                 
[1] FALSE                                       

$params$out.width                               
[1] "100%"                                      

Clearly something is done in rmarkdown:::yaml_front_matter because I see !r being "handled". So my question is: what should I do to evaluate R code, using eval(parse(text=...)) won't work in all generality

Parsing a yaml front matter as rmarkdown is doing would require recreating a logic hidden in rmarkdown and knitr. Especially the handling of params.

The params part are not parsed by rmarkdown::yaml_front_matter() it is handled by knitr directly, using directly yaml::yaml.load and a specific handler for the syntax !r and other.
yaml R package only supports !expr as handler so that code is evaluated when parsing. You could use this syntax in your YAML header.

So I am not sure what you are trying to do, but if you want to recreate how things works you would need to use the same type of handler for yaml.load() than in knitr.

Hope it helps.

Thanks @cderv
The yaml package you mention is https://github.com/vubiostat/r-yaml/ right ?
I lose myself a bit between "r stuff", !r stuff, !expr stuff I think it would benefit better documentation.

It seems !expr stuff is correctly parsed AND evaluated by rmarkdown::yaml_front_matter() (I just tried) as well as yaml::yaml.load, was that what you were saying ?
I think I will just use this going forward
Many thanks for your help

!expr is a built in handler inside the r yaml package (yes the one from your link). So it will work and should be used most of the time, as long as eval.expr = TRUE in the function yaml_load(). See their documentation.

  • knitr can evaluated inline code chunk wirtten like `r expr` - This can be used in any place where evaluation will hapen by knitr for expression to be replaced by their value.
  • !expr is the way to evaluate R code when loading a YAML file. that is yaml package feature.
  • !r is something I have forgotten about and rediscovered with your question TBH. So the form above should be the more generic to use. !r will only work in the params field as this is a possible trhough a custom handler defined in knitr when loading params. Quite specific
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.