Is there a package to process command line options in R?

后端 未结 2 665
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 01:33

Is there a package to process command-line options in R?

I know commandArgs, but it\'s too basic. Its result is basically the equivalent to argc<

相关标签:
2条回答
  • 2021-01-03 02:20

    How about commandArgs with eval for a built in solution?

    test.R

    ## 'trailingOnly=TRUE' means only parse args after '--args'
    args=(commandArgs(trailingOnly=TRUE))
    
    ## Supply default arguments
    if(length(args)==0){
        print("No arguments supplied.")
        ##supply default values
        a = 1
        b = c(1,1,1)
    }else{
        for(i in 1:length(args)){
             eval(parse(text=args[[i]]))
        }
    }
    print(a*2)
    print(b*3)
    

    and to invoke it

    R CMD BATCH --no-save --no-restore '--args a=1 b=c(2,5,6)' test.R test.out
    

    The usual caveats w.r.t using eval apply of course.

    Shamelessly stolen from this blog post.

    0 讨论(0)
  • 2021-01-03 02:22

    getopt for R

    0 讨论(0)
提交回复
热议问题