Always display the current date in an Rmarkdown report

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

comments powered by Disqus