Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YAML current date in rmarkdown

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")`_

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

Any workaround?

like image 653
baptiste Avatar asked Oct 11 '22 10:10

baptiste


People also ask

How do I enter current date in R Markdown?

Instead you can automatically display the current date by placing an inline R chunk containing a call to Sys. Date() as the value of the date field. When knitting the Rmarkdown document Sys. Date() will be evaluated and display the current date.

How get current date in YAML?

In order to do so, you will have to make the date field valid in YAML by quoting the inline R expression, i.e. The parsing error will be gone, and the date will be generated in the markdown output. Pandoc can use the value from Sys. time().

How do I add today's date in R?

To get current date in R, call Sys. Date() function. Sys. Date() returns current date in the current time zone, with an accuracy of milli-second.

What is knitr in R Markdown?

Creating documents with R Markdown starts with an . Rmd file that contains a combination of markdown (content with simple text formatting) and R code chunks. The . Rmd file is fed to knitr, which executes all of the R code chunks and creates a new markdown (. md) document which includes the R code and its output.


4 Answers

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')`"

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().

like image 196
Yihui Xie Avatar answered Oct 24 '22 16:10

Yihui Xie


Just following up on @Yihui. Oddly, I have found that:

'`r format(Sys.Date(), "%B %d, %Y")`'

works better than:

"`r format(Sys.Date(), '%B %d, %Y')`"

For the latter RStudio chooses to change the outer quotes to ' whenever switching between HTML and PDF output and thus breaking the code.

like image 42
John M Avatar answered Oct 24 '22 16:10

John M


Or just single quote the double quotes and vice versa, This works well.

---
title: "Sample Document"
output:
  html_document:
    toc: true
    theme: united
date: '`r format(Sys.time(), "%d %B, %Y")`'
author: baptiste
---
like image 19
SabDeM Avatar answered Oct 24 '22 16:10

SabDeM


One workaround is to use the brew package and write your YAML front matter as a brew template.

---
title: "Sample Document"
output:
  html_document:
    toc: true
    theme: united
date: <%= format(Sys.time(), "%d %B, %Y") %>
author: baptiste
---

You can now use a brew_n_render function that would preprocess the doc using brew and then run in through rmarkdown.

brew_n_render <- function(input, ...){
  output_file <- gsub("\\.[R|r]md$", ".html", input)
  brew::brew(input, 'temp.Rmd');  on.exit(unlink('temp.Rmd'))
  rmarkdown::render('temp.Rmd', output_file = output_file)
}

To make this work with the KnitHTML button in RStudio, you can write a custom output format that will automatically use brew as the preprocessor. Using brew to preprocess ensures that the knitr code chunks in your document are untouched during the preprocessing stage. Ideally, the rmarkdown package should expose the metadata in its API and allow users to run it through a custom function.

like image 12
Ramnath Avatar answered Oct 24 '22 16:10

Ramnath