call RMarkdown on command line using a.R that is passed a file

这一生的挚爱 提交于 2019-12-18 12:56:20

问题


In summary, I am using my script 'Graphs.R' on 'input_file1.txt' in RStudio to create a Rmd which I then knit to html. I would like to automate this process to run more files on the command line.

So far, I can get the Rscript to run on the command line using:

Rscript Graphs.R input_file1.txt

I also know that I can create an .RMD file using:

Rscript -e rmarkdown::render(Graphs.R)

However, I would like to do the following:

Rscript -e rmarkdown::render('Graphs.R input_file1.txt', 'output_file.Rmd')

are there any ideas as how to do this?


回答1:


It's not entirely clear what you are trying to do. It seems like you have a text file which has to be converted to an Rmd by an R script (why isn't it just an Rmd to begin with?) and then you want to render the Rmd. You can do this by running these commands in your terminal:

Rscript Graphs.R
Rscript -e "rmarkdown::render('output_file.Rmd')"

The first command runs the Graphs.R file which presumably generates output_file.Rmd. The second command runs a one-liner which knits output_file.Rmd into output_file.html.

If you want to read command line arguments in an R file, try ?commandArgs.

args <- commandArgs(trailingOnly = TRUE)

Also see this Stack Overflow question.



来源:https://stackoverflow.com/questions/28507693/call-rmarkdown-on-command-line-using-a-r-that-is-passed-a-file

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