Passing command line arguments to R CMD BATCH

前端 未结 6 648
你的背包
你的背包 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:02

    In your R script, called test.R:

    args <- commandArgs(trailingOnly = F)
    myargument <- args[length(args)]
    myargument <- sub("-","",myargument)
    print(myargument)
    q(save="no")
    

    From the command line run:

    R CMD BATCH -4 test.R
    

    Your output file, test.Rout, will show that the argument 4 has been successfully passed to R:

    cat test.Rout
    
    > args <- commandArgs(trailingOnly = F)
    > myargument <- args[length(args)]
    > myargument <- sub("-","",myargument)
    > print(myargument)
    [1] "4"
    > q(save="no")
    > proc.time()
    user  system elapsed 
    0.222   0.022   0.236 
    

提交回复
热议问题