10
votes

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?

1
As a workaround, you could use render and its argument output_file. - CL.
could you please elaborate? - hanshansen
Sorry, what I meant is almost the same as dd3 suggested. - CL.

1 Answers

16
votes

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.