4

我正在尝试通过命令行将多个文件路径参数传递给 Rscript,然后可以使用参数解析器对其进行处理。最终我想要这样的东西

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

通过命令行传递并在解析时将结果作为 R 中的数组

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

我已经尝试了几个解析器,包括 optparse 和 getopt 但它们似乎都不支持这个功能。我知道 argparse 可以,但它目前不适用于 R 版本 2.15.2

有任何想法吗?

谢谢

4

5 回答 5

6

虽然当被问到这个问题时它没有在 CRAN 上发布,但是现在有一个 argparse 模块的 beta 版本可以做到这一点。它基本上是流行的同名 python 模块的包装器,因此您需要安装最新版本的 python 才能使用它。有关详细信息,请参阅安装说明。基本示例包括一个任意长的数字列表,这些数字应该不难修改,因此您可以获取任意长的输入文件列表。

> install.packages("argparse")
> library("argparse")
> example("ArgumentParser")
于 2013-03-01T02:13:52.230 回答
6

您描述命令行选项的方式与大多数人期望使用它们的方式不同。通常,命令行选项将采用单个参数,并且没有前面选项的参数作为参数传递。如果一个参数需要多个项目(如文件列表),我建议使用 strsplit() 解析字符串。

这是一个使用 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)

以下是几个示例运行:

$ 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"     

请注意,对于 strsplit,您可以使用正则表达式来确定分隔符。我会建议如下内容,它可以让您使用逗号或冒号来分隔您的列表:

myfilelist <- strsplit (opt$filelist,"[,:]")
于 2016-08-22T20:40:07.293 回答
4

在你的脚本 test.R 的前面,你把这个:

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)

要得到 :

$inputfiles
[1] "fileA.txt" "fileB.txt" "fileC.txt"

$printvar
[1] "yes"

$size
[1] "10"

$anotheroption
[1] "helloworld"
于 2012-12-09T19:01:34.497 回答
1

在四处搜索并避免自下而上编写新包之后,我认为使用包 optparse 输入多个参数的最佳方法是用一个最有可能非法包含在文件名中的字符分隔输入文件(例如,冒号)

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

文件名中也可以有空格,只要空格被转义(optparse 会处理这个问题)

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

最后,如果有一个支持多个参数的包(可能是 optparse 的修改版本)会很好,就像问题和下面提到的那样

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

人们会认为这些微不足道的功能将被实现到广泛使用的包中,例如 optparse

干杯

于 2012-12-10T23:57:18.437 回答
0

如果输入参数是相同长度的列表,@agstudy 的解决方案将无法正常工作。默认情况下, sapply 会将相同长度的输入折叠成矩阵而不是列表。修复很简单,只需在 sapply 解析参数时显式设置 simple 为 false 。

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)
于 2014-12-11T20:23:07.230 回答