In markdown files, yaml syntax configuration contains Front-matter. Rmarkdown is one type markdown type used to generate reports with R language.
This tutorial tells how to add a current date in the yaml formatter in rmarkdown files.
Suppose you have yaml front matter as follows.
---
date: 2022-04-08T00:00:00-00:00
title: "YAML - Current Date in rmarkdown"
output: html_document
author: Renuka
---
You want to replace the date field with the current date produced with the r expression.
IN R language, Sys.Date() returns the current date in yyyy-mm-dd
---
date: "`R Sys.Date()`"
title: "YAML - Current Date in rmarkdown"
output: html_document
author: Renuka
---
Generates the
---
date: 2022-04-08
title: "YAML - Current Date in rmarkdown"
output: html_document
author: Renuka
---
if you want a different format in reports, You can use the format function with the desired format.
format(Sys.Date(), "%d-%m-%Y") - 16-07-2021
format(Sys.Date(), "%d %B %Y") - 16 July 2021
The same can be done using the R format function in yaml syntax as below.
---
date: "`r format(Sys.Date(), "%d %B %Y")`"
title: "YAML - Current Date in rmarkdown"
output: html_document
author: Renuka
---
Please note the R expression enclosed in ` quote that is wrapped inside double quotes.