10

In my yaml call I have

---
title: "`r paste0('Test. Done ', format(Sys.Date(), '%B-%Y'))`"
output:
  word_document:
    fig_caption: yes
    fig_height: 4
    fig_width: 7
    reference_docx: %userprofile%\Documents\template.docx
---

But YAML complains about %userprofile%. Is it possible to include such a variable?

I have tried e.g.

reference_docx: "`r file.path(path.expand('~'), 'skabelon.docx')`"

But that still results in this YAML error.

pandoc.exe: `r file.path(path.expand('~'), 'skabelon.docx')`: openBinaryFile: does not exist (No such file or directory)

I guess this meens that the r expression is not processed before the yaml? I have checked that the file is there... Or is it becayse pandoc is using another 'userprofile' ? how can I check this?

I can however use such a call in the Title variable, as per the updated title above. I guess this must be a specific knitr issue.

bschneidr
  • 6,014
  • 1
  • 37
  • 52
Andreas
  • 6,612
  • 14
  • 59
  • 69

2 Answers2

11

The problem is that the parameters in the output field is only understood by rmarkdown. Pandoc does not understand them, so you need to make sure rmarkdown can evaluate the expression. Since rmarkdown uses the yaml package to read the YAML metadata, and yaml's syntax for R expressions is !expr, you can put the R expression after !expr, e.g.

output:
  word_document:
    fig_caption: yes
    fig_height: 4
    fig_width: 7
    reference_docx: !expr file.path(Sys.getenv('USERPROFILE'), 'Documents', 'template.docx')
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 2
    This works @Yihui. But does this mean that "!expr" supersedes "`r"? I can find this documented in the yaml package - but not elsewhere. Iin my example "'r" works for date fields but !expr does not. On the other hand !expr works for reference_docx but "'r" does not? Whats the difference? – Andreas Dec 01 '16 at 12:54
  • 2
    Good question. The `r` syntax is only understood by knitr. In your case, you want rmarkdown to understand it is an R expression. Since rmarkdown uses yaml to parse the YAML metadata, and yaml can only understand `!expr`. – Yihui Xie Dec 01 '16 at 18:56
  • 1
    This trips me up every time. I can use "`r Sys.time()`" in the date field of YAML, but have to use `!expr path_to_doc.docx` for a reference template. knitr + RMd is great, but this is unintuitive and non-obvious, to me at least. – Matt Cowgill Jul 14 '21 at 22:55
  • 1
    @MattCowgill I definitely agree with you. – Yihui Xie Jul 16 '21 at 19:38
  • 1
    In any case thank you for all your packages @Yihui, you’ve made a huge contribution. – Matt Cowgill Jul 17 '21 at 21:50
0

You can include such a variable, but you should quote the scalar in which put it (either single quotes or double quotes). Within a quoted scalar the % doesn't have any special meaning only the single quote has (in single quoted strings, or the backslash (\) in double quoted strings.

Any further interpretation of that scalar as Windows variable is outside of the scope of YAML and needs to be done by your program.

The scalar:

"`r file.path(path.expand('~'), 'skabelon.docx')`"

is correct YAML and shout not throw an error (but of course is nothing else for YAML than a buch of characters starting and ending with a backquote, no interpretation should be done on that by the parser).

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • Okay - thanks. For now I am assuming its a knitr issue – Andreas Nov 25 '16 at 10:51
  • The YAML is completely parsed and then handed to the knitr, which expects a certain syntax it looks like it doesn't do anything with the backquotes. I would first hard-code the correct path in the YAML scalar without any backquotes and then gradually replace parts with things that knitr can evaluate, every time checking that it still works. – Anthon Nov 25 '16 at 11:44
  • If I replace the title value with the value for reference_docx, and hardcode the output of the call in to the reference_docx value, the correct path is printed in the title (like this: https://i.imgur.com/5JkCVoN.png). So I guess it must be a knitr issue. – Andreas Nov 25 '16 at 14:05