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

查看:29
本文介绍了在 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	emplate.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)

我猜这意味着 r 表达式没有在 yaml 之前处理?我已经检查过该文件是否存在......还是因为 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 变量中使用这样的调用.我想这一定是一个特定的 knitr 问题.

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,可以把R表达式放在!expr之后,eg

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天全站免登陆