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

前端 未结 6 2014
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  醉话见心
    2020-12-08 23:47

    If you're interested in using e.g. argparser and continue developing/analyzing interactively using Rstudio, you can use the following work-around:

    1. Write your command line parser in my_rscript and create an object args that contains all parsed input.
    2. Add a line that saves args object to file.
    3. Run my_rscript from command line and specify arguments of interest.
    4. Load the args object from file in Rstudio and continue interactively

    Example:

    library("argparser")
    parser <- arg_parser(description='Process commandline arguments')
    parser <- add_argument(parser, arg="--dataset", type="character", help = "Dataset argument")
    args = parse_args(parser)
    args_file = "tempArgObjectFile.rds"
    saveRDS(args, args_file); print(args); quit(); #comment this after creating args_file
    args = readRDS(args_file)  #use this to load during interactive development
    

提交回复
热议问题