Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple arguments via command line in R

I am trying to pass multiple file path arguments via command line to an Rscript which can then be processed using an arguments parser. Ultimately I would want something like this

Rscript test.R --inputfiles fileA.txt fileB.txt fileC.txt --printvar yes --size 10 --anotheroption helloworld -- etc...

passed through the command line and have the result as an array in R when parsed

args$inputfiles =  "fileA.txt", "fileB.txt", "fileC.txt"

I have tried several parsers including optparse and getopt but neither of them seem to support this functionality. I know argparse does but it is currently not available for R version 2.15.2

Any ideas?

Thanks

like image 719
Omar Wagih Avatar asked Dec 04 '22 01:12

Omar Wagih


1 Answers

Although it wasn't released on CRAN when this question was asked a beta version of the argparse module is up there now which can do this. It is basically a wrapper around the popular python module of the same name so you need to have a recent version of python installed to use it. See install notes for more info. The basic example included sums an arbitrarily long list of numbers which should not be hard to modify so you can grab an arbitrarily long list of input files.

> install.packages("argparse")
> library("argparse")
> example("ArgumentParser")
like image 127
Trevor Avatar answered Dec 23 '22 09:12

Trevor