使用RStudio Knit按钮在编织文档时在文件名中插入日期

han*_*sen 7 r rstudio knitr r-markdown

我想在使用RStudio的编织按钮编织文档时在输出文件名中包含当前日期.我可以以某种方式改变降价渲染的选项,但我不知道如何.谁能指出我正确的方向?

ble*_*lep 13

您可以在控制台中执行此操作:

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

替代,更好的版本:

rmarkdown::render("test.Rmd",output_file=paste0('test',Sys.Date(),'.html'))
Run Code Online (Sandbox Code Playgroud)

您可以使用文档中的某些代码直接更改RStudio编织按钮的行为,如下所示.

在标题中,在输出部分之前添加此代码:

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

substr(inputFile,1, nchar(inputFile)-4)带您RMD文件名".Rmd".

  • @ user3908149 scratch - 您可以在markdown文档中插入一些代码以动态命名输出文件,请参阅:http://stackoverflow.com/questions/28500096/r-markdown-variable-output-name (2认同)