YAML current date in rmarkdown

RYamlKnitrR Markdown

R Problem Overview


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?

R Solutions


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

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

Solution 2 - R

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.

Solution 3 - R

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
---

Solution 4 - R

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.

Solution 5 - R

or, perhaps something like the following, see R Markdown Parameterized Reports

params:
  reportDate:
    input: date
    label: 'Report Date:'
    value: as.POSIXct(Sys.Date())

Solution 6 - R

enter image description hereFor the same problem for me. I solve it using this code .

---
title: "bla bla"
author: "My name"
date: \`r format(Sys.Date(), "%B %d, %Y")`\
output: html_document
---

Update You can use another format too .

---
title: "bla bla"
author: "My name"
date: \`r format(Sys.Date(), "%m %d,%Y")`\
output: html_document
---

Best.

Solution 7 - R

I was bitten by this today. I had

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

and got more or less the same error as the OP, but only when knitting to word. Knitting to pdf was fine before I tried knitting to Word. Afterwards it didn't work either.

Error in yaml::yaml.load(front_matter) : 
  Scanner error: while scanning for the next token at line 3, column 31
 found character that cannot start any token at line 3, column 31
Calls: <Anonymous> ... output_format_from_yaml_front_matter -> 
       parse_yaml_front_matter -> <Anonymous> -> .Call`

Position 31 is the first % sign

Replacing this with

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

as advised by MLaVoie, worked fine.

I have no idea why this happened, and I don't have time to go digging - reports to finish.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbaptisteView Question on Stackoverflow
Solution 1 - RYihui XieView Answer on Stackoverflow
Solution 2 - RJohn MView Answer on Stackoverflow
Solution 3 - RSabDeMView Answer on Stackoverflow
Solution 4 - RRamnathView Answer on Stackoverflow
Solution 5 - RJustAnotherView Answer on Stackoverflow
Solution 6 - RSaber bouabidView Answer on Stackoverflow
Solution 7 - RastainesView Answer on Stackoverflow