315

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?

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • 7
    I'm surprised that this no longer works, since this is exactly what I used to do. I'll see what was going on recently. BTW, yaml also supports values computed from R via `!expr`, e.g. `date: !expr Sys.time()`, but now this does not work either. – Yihui Xie May 07 '14 at 20:59

8 Answers8

445

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

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 2
    I guess this means inline R can't be used for e.g. a list `includes: after_body: [ ... ]` as valid YAML would only be filename strings... So no possibility of ```includes: "`r list.files(...)`"``` ? – Louis Maddox Mar 02 '15 at 18:33
  • 1
    @Yihui this works for me in the html output, but not in the resulting `.md` file if I have `keep_md: true` in the YAML header. Any solution for this? – Matt SM Sep 25 '15 at 22:12
  • 9
    For any other Americans: `date: "\`r format(Sys.time(), '%B %d, %Y')\`"`. – ubomb Aug 25 '16 at 17:46
  • Hi @Yihui, I got garbled month in date output in the pdf file. Do you have any idea how can I solve this issue? Thank you. – HW-Scientist Mar 30 '18 at 06:29
  • 3
    If you need a dot in the date, keep in mind to escape them and to escape the escapes: `r format(Sys.time(), '%d\\\\. %B %Y')` – BurninLeo Mar 12 '19 at 08:15
  • what is `r` here? – Jananath Banuka Feb 05 '20 at 14:35
  • @JananathBanuka Syntax for inline R expressions: https://bookdown.org/yihui/rmarkdown/basics.html – Yihui Xie Feb 05 '20 at 18:36
  • This command will show the month using the default language of the system, but would it be possible to show the month on a different language? For example, if my system has English as default language, could the month be shown in, say, French? – ForEverNewbie Oct 28 '20 at 08:24
  • Is it possible to get current date + 3 days? – mikolaj semeniuk Aug 16 '23 at 10:10
  • 1
    @mikolajsemeniuk `format(Sys.Date() + 3, '%d %B, %Y')` – Yihui Xie Aug 16 '23 at 12:08
87

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.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
John M
  • 1,115
  • 8
  • 10
19

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
---
SabDeM
  • 7,050
  • 2
  • 25
  • 38
12

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.

Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • 5
    thanks Ramnath, that would work. It would be nice not to have the extra steps and temp files in the workflow; in my experience the more convoluted the process, the less it is reproducible (i.e. I can't remember how it works) a few months later. – baptiste May 04 '14 at 18:56
9

Alternatively, if you use the new format quarto https://quarto.org. you can do this in your yaml header of a .qmd file.

——-
title: title of my doc
date: today
format:
  html:
    theme: default
——-

For formatting, check formating options here.

As an example,

date: 03/07/2005
date-format: long
Matias Andina
  • 4,029
  • 4
  • 26
  • 58
olivroy
  • 548
  • 3
  • 13
4

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

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

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.

Saber bouabid
  • 117
  • 1
  • 11
-1

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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
astaines
  • 872
  • 2
  • 9
  • 20