Passing command line arguments to R CMD BATCH

前端 未结 6 658
你的背包
你的背包 2020-11-27 09:22

I have been using R CMD BATCH my_script.R from a terminal to execute an R script. I am now at the point where I would like to pass an argument to

6条回答
  •  青春惊慌失措
    2020-11-27 10:03

    I add an answer because I think a one line solution is always good! Atop of your myRscript.R file, add the following line:

    eval(parse(text=paste(commandArgs(trailingOnly = TRUE), collapse=";")))
    

    Then submit your script with something like:

    R CMD BATCH [options] '--args arguments you want to supply' myRscript.R &
    

    For example:

    R CMD BATCH --vanilla '--args N=1 l=list(a=2, b="test") name="aname"' myscript.R &
    

    Then:

    > ls()
    [1] "N"    "l"    "name"
    

提交回复
热议问题