rcommand-lineargumentsargparseoptparse

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


Solution

  • After searching around, and avoiding to write a new package from the bottom up, I figured the best way to input multiple arguments using the package optparse is to separate input files by a character which is most likely illegal to be included in a file name (for example, a colon)

    Rscript test.R --inputfiles fileA.txt:fileB.txt:fileC.txt etc...
    

    File names can also have spaces in them as long as the spaces are escaped (optparse will take care of this)

    Rscript test.R --inputfiles file\ A.txt:file\ B.txt:fileC.txt etc...
    

    Ultimatley, it would be nice to have a package (possibly a modified version of optparse) that would support multiple arguments like mentioned in the question and below

    Rscript test.R --inputfiles fileA.txt fileB.txt fileC.txt
    

    One would think such trivial features would be implemented into a widely used package such as optparse

    Cheers