Insert date in filename while knitting document using RStudio Knit button

冷暖自知 提交于 2019-12-07 01:48:41

问题


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:


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.



来源:https://stackoverflow.com/questions/32377291/insert-date-in-filename-while-knitting-document-using-rstudio-knit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!