在YAML中使用R代码或Windows用户变量(“%userprofile%")? [英] Use R code or Windows user variable ("%userprofile%") in YAML?

查看:147
本文介绍了在YAML中使用R代码或Windows用户变量(“%userprofile%")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Yaml通话中,

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
---

但是YAML抱怨%userprofile%.可以包含这样的变量吗?

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

我尝试过

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

但这仍然会导致此YAML错误.

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)

我想这意味着在yaml之前不处理r表达式吗?我检查了文件是否存在...或者是因为pandoc使用了另一个用户配置文件"?我该如何检查?

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?

但是,按照上面更新的标题,我可以在Title变量中使用这样的调用.我想这一定是一个特定的针织问题.

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.

推荐答案

问题是output字段中的参数只能由 rmarkdown 理解. Pandoc无法理解它们,因此您需要确保 rmarkdown 可以评估该表达式.由于 rmarkdown 使用 yaml 包读取YAML元数据,并且 yaml 的R表达式语法为!expr,因此您可以将!expr之后的R表达式,例如

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')

这篇关于在YAML中使用R代码或Windows用户变量(“%userprofile%")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆