Passing multiple arguments via command line in R

前端 未结 5 1207
佛祖请我去吃肉
佛祖请我去吃肉 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: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"
    

提交回复
热议问题