5

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')`
---
Nicholas Hamilton
  • 10,044
  • 6
  • 57
  • 88

1 Answers1

4

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))
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Perfect, just what I needed. I should also say, after experimentation, if `pdf_document(...)` is used in preference to 'pdf_document' (ie function in preference to character string), then the function version will override anything specified in the YAML header. – Nicholas Hamilton Jun 14 '16 at 02:28