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

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

I know commandArgs, but it's too thorough. Its result is basically equivalent to argcand argvin C, but I need something beyond that, just like boost::program_optionsin C++or GetOptions::Longin perl.

In particular, I would like to indicate in advance which parameters are allowed, and give an error message if the user indicates something else.

The call will be like this (with custom parameters - width = 32 --file = foo.txt):

R --vanilla --args --width=32 --file=foo.txt < myscript.R

or, if used Rscript:

myscript.R --width=32 --file=foo.txt 

(Please don’t say: “Why don’t you write it yourself, it’s not so difficult.” In other languages, you don’t need to write either. :)

+5
2
+8

commandArgs eval ?

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)

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

w.r.t eval , .

.

+2

All Articles