rmarkdown 中的 YAML 当前日期 [英] YAML current date in rmarkdown
问题描述
我想知道是否有一个技巧可以将当前日期放在 .rmd
文档的 YAML 前端,以由 knitr
和 处理>rmarkdown
包.我曾经在我的 wiki 页面顶部有以下行,
I'm wondering if there's a trick to put the current date in the YAML front-matter of a .rmd
document to be processed by knitr
and the rmarkdown
package. I used to have the following line at the top of my wiki pages,
_baptiste, `r format(Sys.time(), "%d %B, %Y")`_
它会在 html 输出中转换为 baptiste, 03 May, 2014.现在,我想利用 rmarkdown
提供的高级 pandoc 包装器,但是在 YAML 标头中包含 r 代码似乎不起作用:
and it would get converted to baptiste, 03 May, 2014 in the html output. Now, I would like to take advantage of the advanced pandoc wrapper provided by rmarkdown
, but having r code in the YAML header doesn't seem to work:
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
date: `r format(Sys.time(), "%d %B, %Y")`
author: baptiste
---
Error in yaml::yaml.load(front_matter) :
Scanner error: while scanning for the next token at line 6, column 7
found character that cannot start any token at line 6, column 7
Calls: <Anonymous> ... output_format_from_yaml_front_matter ->
parse_yaml_front_matter -> <Anonymous> -> .Call
有什么解决方法吗?
推荐答案
这有点棘手,但您只需通过引用内联 R 表达式使 date
字段在 YAML 中有效,例如
This is a little bit tricky, but you just need to make the date
field valid in YAML by quoting the inline R expression, e.g.
date: "`r format(Sys.time(), '%d %B, %Y')`"
然后解析错误将消失,并且日期将在markdown输出中生成,因此Pandoc可以使用来自Sys.time()
的值.
Then the parsing error will be gone, and the date will be generated in the markdown output so Pandoc can use the value from Sys.time()
.
这篇关于rmarkdown 中的 YAML 当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!