Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R markdown YAML dynamic variables

Tags:

In RMarkdown, I seem to be able to create 'some' dynamic variables in the YAML header, but not for others:

For instance, this works:

---
title: 
  "Some Title, `r format(Sys.time(), '%d %B, %Y')`"
...
---

But this does NOT.

---
...
pdf_document:
    keep_tex: `r 'true'`
---

But this DOES (ie not dynamic).

---
...
pdf_document:
    keep_tex: true
---

So how can I 'dynamically' assign the keep_tex to either true or false, what I want to do, is something like this:

---
...
pdf_document:
    keep_tex: `r getOption('mypackage.keep_tex')`
---
like image 885
Nicholas Hamilton Avatar asked Jun 13 '16 04:06

Nicholas Hamilton


1 Answers

I don't know if the template options can be set programmatically in the YAML header of the .Rmd file.

As an alternative, if you use rmarkdown::render to render your document, you may specify the output template (pdf_document), and then set template options (e.g. keep_tex) programmatically.

For example, if you have a .Rmd file called "test.Rmd" like this:

---
title: 
  "Some Title, `r format(Sys.time(), '%d %B, %Y')`"
---

...and some logical object which determines whether to keep the intermediate TeX file or not, e.g.

my_keep <- TRUE

...you may render the input file to PDF format and keep the TeX file like this:

render(input = "test.Rmd",
       output_format = pdf_document(keep_tex = my_keep))
like image 200
Henrik Avatar answered Sep 28 '22 02:09

Henrik