通过R中的命令行传递多个参数 [英] Passing multiple arguments via command line in R
问题描述
我正在尝试通过命令行将多个文件路径参数传递给Rscript,然后可以使用参数解析器对其进行处理.最终我会想要这样的东西
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...
通过命令行传递,并在解析时将结果显示为R中的数组
passed through the command line and have the result as an array in R when parsed
args$inputfiles = "fileA.txt", "fileB.txt", "fileC.txt"
我尝试了几种解析器,包括optparse和getopt,但它们都不支持此功能.我知道argparse可以,但是目前不适用于R版本2.15.2
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
有什么想法吗?
谢谢
推荐答案
经过四处搜索,并避免了从头开始编写新程序包,我发现使用optparse程序包输入多个参数的最佳方法是分离输入文件中包含的字符很可能是非法字符(例如冒号)
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...
文件名中也可以包含空格,只要转义空格即可(optparse会解决这个问题)
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,最好有一个程序包(可能是optparse的修改版本),该程序包支持问题和下文中提到的多个参数.
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
人们会认为这样的琐碎功能将被实现为广泛使用的软件包,例如optparse
One would think such trivial features would be implemented into a widely used package such as optparse
欢呼
这篇关于通过R中的命令行传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!