13

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?

blep
  • 726
  • 1
  • 11
  • 28
hanshansen
  • 623
  • 5
  • 8

1 Answers1

22

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.

Community
  • 1
  • 1
blep
  • 726
  • 1
  • 11
  • 28
  • 1
    thanks a lot, but i would prefer to get this automatically done when i press the knit button. – hanshansen Sep 03 '15 at 20:41
  • 1
    @user3908149 if you click the little gear next to the knit button, you can see the options available to you using the GUI. – blep Sep 03 '15 at 21:23
  • 2
    @user3908149 scratch that - you can insert some code into your markdown document to dynamically name the output file, see: http://stackoverflow.com/questions/28500096/r-markdown-variable-output-name – blep Sep 03 '15 at 21:27
  • I'm trying to do the same thing with parameters but it doesn't work - the `knit_to_parameters:` part of the YAML header is ignored and the output html file still has the same name as the .Rmd – Amy M Oct 25 '18 at 18:43