Is it possible to specify command line parameters to R-script in RStudio?

前端 未结 6 1993
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 23:10

I want to use RStudio to edit an R-script having command line parameters, e.g.,

my_rscript --dataset mydataset

and then to read the optiion

6条回答
  •  萌比男神i
    2020-12-08 23:33

    This worked for me: My Rscript is as follows:

    args <- commandArgs()
    print(paste0("args:", args))
    print(paste0("args[1]:",args[1]))
    print(paste0("args[2]:",args[2]))
    print(paste0("args[3]:",args[3]))
    print(paste0("# of args:",length(args)))
    

    '

    To emulate the command line input I would use with Rscript, I entered this in RStudio:

    commandArgs <- function() c("AMZN", 10, 200)

    which gave the desired result:

    [1] "args:AMZN" "args:10"   "args:200" 
    [1] "args[1]:AMZN"
    [1] "args[2]:10"
    [1] "args[3]:200"
    [1] "# of args:3"
    

提交回复
热议问题