Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert date in filename while knitting document using RStudio Knit button

I would like to include the current date in the output filename when knitting a document using RStudio's knit button. I can somehow change the options of the markdown rendering, but I don't know how. Could anyone point me into the right direction?

like image 988
hanshansen Avatar asked Sep 03 '15 13:09

hanshansen


1 Answers

You can do this in the console:

library(knitr)  
knit("test.Rmd")
knit2html("test.md", output=paste0("test",Sys.Date(),".html")) # Sys.Date() is a string with the current date

Alternate, better version:

rmarkdown::render("test.Rmd",output_file=paste0('test',Sys.Date(),'.html'))

You can directly change the behavior of the RStudio knit button with some code in your document, like this.

To the header, before the output section add this code:

knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = paste0(substr(inputFile,1,nchar(inputFile)-4),Sys.Date(),'.html')) })

The substr(inputFile,1, nchar(inputFile)-4) strips the ".Rmd" from your Rmd filename.

like image 159
blep Avatar answered Oct 09 '22 03:10

blep