如何将 here() 用于 css、before_body 和 after_body 的路径?
How to use here() for paths to css, before_body and after_body?
我有一组分布在文件夹层次结构中的 RMarkdown 文档。所有文档均引用相同的 CSS 和 header/footer 文件。我目前硬编码了这些文件的路径,但这很难维护。我更喜欢动态生成路径。
这有效(日期是在代码中动态生成的):
---
title: "Untitled"
date: "`r Sys.Date()`"
output: html_document
---
但这不起作用:
---
title: "Untitled"
date: "`r Sys.Date()`"
output:
html_document:
css: '`r here::here("styles/styles.css")`'
includes:
before_body: '`r here::here("styles/header.html")`'
after_body: '`r here::here("styles/footer.html")`'
---
文件 styles.css
、header.html
和 footer.html
都位于项目根目录下的 styles/
文件夹中。当我从控制台使用 here::here()
引用这些文件时,它工作得很好。
但是,当我编译 RMarkdown 时,出现如下错误:
File `r here::here( not found in resource path
Error: pandoc document conversion failed with error 99
以上错误与 CSS 文件有关。然后是页眉和页脚:
pandoc: `r here::here("styles/header.html")`: openBinaryFile: does not exist
(No such file or directory)
这段代码已经达到 Pandoc 的程度这一事实向我表明代码块没有被评估。
我是不是在做傻事?这是预期的行为吗?在我看来,能够动态生成路径将非常有帮助。
使用 yaml::yaml.load()
函数解析 YAML header。
该函数的手册页解释说
There is a built-in handler that will evaluate expressions that are tagged with the !expr
tag. Currently this handler is enabled by default, but the handler is being disabled by default in an upcoming version for safety and security reasons. If you do not explicity set the eval.expr
argument to TRUE
, you will get a warning if an expression is evaluated. Alternately, you can set the option named yaml.eval.expr
via the options
function to turn off the warning.
因此,您可以使用此 YAML 实现您的目标 header:
---
title: "Untitled"
date: "`r Sys.Date()`"
output:
html_document:
css: !expr here::here("styles/styles.css")
includes:
before_body: !expr here::here("styles/header.html")
after_body: !expr here::here("styles/footer.html")
---
我有一组分布在文件夹层次结构中的 RMarkdown 文档。所有文档均引用相同的 CSS 和 header/footer 文件。我目前硬编码了这些文件的路径,但这很难维护。我更喜欢动态生成路径。
这有效(日期是在代码中动态生成的):
---
title: "Untitled"
date: "`r Sys.Date()`"
output: html_document
---
但这不起作用:
---
title: "Untitled"
date: "`r Sys.Date()`"
output:
html_document:
css: '`r here::here("styles/styles.css")`'
includes:
before_body: '`r here::here("styles/header.html")`'
after_body: '`r here::here("styles/footer.html")`'
---
文件 styles.css
、header.html
和 footer.html
都位于项目根目录下的 styles/
文件夹中。当我从控制台使用 here::here()
引用这些文件时,它工作得很好。
但是,当我编译 RMarkdown 时,出现如下错误:
File `r here::here( not found in resource path
Error: pandoc document conversion failed with error 99
以上错误与 CSS 文件有关。然后是页眉和页脚:
pandoc: `r here::here("styles/header.html")`: openBinaryFile: does not exist
(No such file or directory)
这段代码已经达到 Pandoc 的程度这一事实向我表明代码块没有被评估。
我是不是在做傻事?这是预期的行为吗?在我看来,能够动态生成路径将非常有帮助。
使用 yaml::yaml.load()
函数解析 YAML header。
该函数的手册页解释说
There is a built-in handler that will evaluate expressions that are tagged with the
!expr
tag. Currently this handler is enabled by default, but the handler is being disabled by default in an upcoming version for safety and security reasons. If you do not explicity set theeval.expr
argument toTRUE
, you will get a warning if an expression is evaluated. Alternately, you can set the option namedyaml.eval.expr
via theoptions
function to turn off the warning.
因此,您可以使用此 YAML 实现您的目标 header:
---
title: "Untitled"
date: "`r Sys.Date()`"
output:
html_document:
css: !expr here::here("styles/styles.css")
includes:
before_body: !expr here::here("styles/header.html")
after_body: !expr here::here("styles/footer.html")
---