Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create Rmd template that auto generates the date just like default Rmd template

Tags:

r

knitr

r-package

In RStudio, when I open a new R Markdown file, the default format has this nice feature where it writes in today's date into the YAML.

I would love for my custom templates in my own R packages to offer this feature but I cannot seem to do better than

---
title: ""
author: "Me"
date: '`r format(Sys.Date(), "%Y-%B-%d")`'
output: html_document
---

The problem with 'r format(Sys.Date(), "%Y-%B-%d")' is that it rewrites the date every time I knit the file, which is undesirable for my workflow. I'd rather maintain the date that the file was initiated.

like image 538
Joe Avatar asked Jan 24 '26 12:01

Joe


1 Answers

You could use file.info like this

---
title: "title" 
author: "Me"
date: '`r file.info("date.Rmd")$ctime`'
output: html_document
---
alternatively...
`r file.info(knitr::current_input())$ctime`

Assuming your markdown file is called date.Rmd.

Edited to add a more general method...

like image 159
Andrew Chisholm Avatar answered Jan 26 '26 02:01

Andrew Chisholm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!