本文介绍了Rmarkdown中的YAML当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有技巧将当前日期放在要由knitrrmarkdown包处理的.rmd文档的YAML前端.我曾经在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,2014年5月3日.现在,我想利用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表达式(例如

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当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:41