gpt4 book ai didi

r - 在YAML中使用R代码或Windows用户变量("%userprofile%")?

转载 作者:行者123 更新时间:2023-12-03 17:05:14 26 4
gpt4 key购买 nike

在我的Yaml电话中,我有

---
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%。是否可以包含这样的变量?

我已经尝试过
reference_docx: "`r file.path(path.expand('~'), 'skabelon.docx')`"

但这仍然会导致此YAML错误。
pandoc.exe: `r file.path(path.expand('~'), 'skabelon.docx')`: openBinaryFile: does not exist (No such file or directory)

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

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

最佳答案

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

output:
word_document:
fig_caption: yes
fig_height: 4
fig_width: 7
reference_docx: !expr file.path(Sys.getenv('USERPROFILE'), 'Documents', 'template.docx')

关于r - 在YAML中使用R代码或Windows用户变量("%userprofile%")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40801633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com