Passing multiple arguments via command line in R

前端 未结 5 1083
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 07:49

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

相关标签:
5条回答
  • 2021-01-06 08:14

    @agstudy's solution does not work properly if input arguments are lists of the same length. By default, sapply will collapse inputs of the same length into a matrix rather than a list. The fix is simple enough, just explicitly set simplify to false in the sapply parsing the arguments.

    args <- commandArgs(trailingOnly = TRUE)
    
    hh <- paste(unlist(args),collapse=' ')
    listoptions <- unlist(strsplit(hh,'--'))[-1]
    options.args <- sapply(listoptions,function(x){
             unlist(strsplit(x, ' '))[-1]
            }, simplify=FALSE)
    options.names <- sapply(listoptions,function(x){
      option <-  unlist(strsplit(x, ' '))[1]
    })
    names(options.args) <- unlist(options.names)
    print(options.args)
    
    0 讨论(0)
  • 2021-01-06 08:16

    In the front of your script test.R, you put this :

    args <- commandArgs(trailingOnly = TRUE)
    
    hh <- paste(unlist(args),collapse=' ')
    listoptions <- unlist(strsplit(hh,'--'))[-1]
    options.args <- sapply(listoptions,function(x){
             unlist(strsplit(x, ' '))[-1]
            })
    options.names <- sapply(listoptions,function(x){
      option <-  unlist(strsplit(x, ' '))[1]
    })
    names(options.args) <- unlist(options.names)
    print(options.args)
    

    to get :

    $inputfiles
    [1] "fileA.txt" "fileB.txt" "fileC.txt"
    
    $printvar
    [1] "yes"
    
    $size
    [1] "10"
    
    $anotheroption
    [1] "helloworld"
    
    0 讨论(0)
  • 2021-01-06 08:22

    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")
    
    0 讨论(0)
  • 2021-01-06 08:29

    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

    0 讨论(0)
  • 2021-01-06 08:31

    The way you describe command line options is different from the way that most people would expect them to be used. Normally, a command line option would take a single parameter, and parameters without a preceding option are passed as arguments. If an argument would take multiple items (like a list of files), I would suggest parsing the string using strsplit().

    Here's an example using optparse:

    library (optparse)
    option_list <- list ( make_option (c("-f","--filelist"),default="blah.txt", 
                                       help="comma separated list of files (default %default)")
                         )
    
    parser <-OptionParser(option_list=option_list)
    arguments <- parse_args (parser, positional_arguments=TRUE)
    opt <- arguments$options
    args <- arguments$args
    
    myfilelist <- strsplit(opt$filelist, ",")
    
    print (myfilelist)
    print (args)
    

    Here are several example runs:

    $ Rscript blah.r -h
    Usage: blah.r [options]
    
    
    Options:
        -f FILELIST, --filelist=FILELIST
            comma separated list of files (default blah.txt)
    
        -h, --help
            Show this help message and exit
    
    
    $ Rscript blah.r -f hello.txt
    [[1]]
    [1] "hello.txt"
    
    character(0)
    $ Rscript blah.r -f hello.txt world.txt
    [[1]]
    [1] "hello.txt"
    
    [1] "world.txt"
    $ Rscript blah.r -f hello.txt,world.txt another_argument and_another
    [[1]]
    [1] "hello.txt" "world.txt"
    
    [1] "another_argument" "and_another"
    $ Rscript blah.r an_argument -f hello.txt,world.txt,blah another_argument and_another
    [[1]]
    [1] "hello.txt" "world.txt" "blah"     
    
    [1] "an_argument"      "another_argument" "and_another"     
    

    Note that for the strsplit, you can use a regular expression to determine the delimiter. I would suggest something like the following, which would let you use commas or colons to separate your list:

    myfilelist <- strsplit (opt$filelist,"[,:]")
    
    0 讨论(0)
提交回复
热议问题