Always display the current date in an Rmarkdown report

[This article was first published on R on Thomas' adventuRe, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Rmarkdown is a great tool for creating data-driven reports that have to be updated on a regular basis. To communicate to stakeholders that the report is indeed up to date it’s important to display the current date below the title.

An obvious solution to display the current date is to update the date field in the YAML header manually before knitting. But this is both error prone and you are likely to forget about it. Instead you can dislay the current date in an automatic way using the method below.

---
title: "Super Important Report"
author: "Thomas Neitmann"
date: `r Sys.Date()`
---

When knitting the Rmarkdown document Sys.Date() will be evaluated and display the current date.

Sys.Date()
## [1] "2020-03-22"

A caveat is the date format, though. Sys.Date() returns the current date in ISO 8601 format, i.e. yyyy-mm-dd. Your audience is likely not used to this format so you might want to use a date format that’s common in your locale.

How to achieve that? Using the format() function. Here are some examples.

current_date <- Sys.Date()
format(current_date, "%d.%m.%Y")
## [1] "22.03.2020"
format(current_date, "%d. %B %Y")
## [1] "22. März 2020"
format(current_date, "%m/%d/%Y")
## [1] "03/22/2020"

After deciding which format to use make sure to wrap Sys.Date() inside of format() in your YAML header.

---
title: "Super Important Report"
author: "Thomas Neitmann"
date: `r format(Sys.Date(), "%d. %B %Y")`
---

To leave a comment for the author, please follow the link and comment on their blog: R on Thomas' adventuRe.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)