简体   繁体   English

R命令行将文件名传递给参数中的脚本(Windows)

[英]R command line passing a filename to script in arguments (Windows)

I'm having a hard time passing a filename to my R script. 我很难将文件名传递给我的R脚本。 The file is a csv file with the batch parameters for multiple runs of the script. 该文件是一个csv文件,其中包含多次运行脚本的批处理参数。 I am trying to include it here so that the user does not need to edit the R script in order to specify the location of that file. 我试图在此处包含它,以便用户不需要编辑R脚本以指定该文件的位置。

My Windows command line syntax is: 我的Windows命令行语法是:

R CMD BATCH --slave "--args fn=batch.csv" myscript.r output.txt

The closest I have gotten to retrieving this in my R script is by doing: 我在R脚本中检索到的最接近的是:

eval(parse(file=commandArgs()[8])))
batch_args = read.table(fn, sep=",")

I have experimented with commandArgs(trailingOnly=TRUE) and parse(text=commandArgs()[8]) , etc., with no luck. 我已经尝试过commandArgs(trailingOnly=TRUE)parse(text=commandArgs()[8])等,没有运气。 Most of the documentation that I have seen does not apply specifically to passing filenames. 我见过的大多数文档都不适用于传递文件名。 Can anyone think of a solution? 谁能想到解决方案?

As I said in my comment, I would use Rscript instead of R CMD BATCH : 正如我在评论中所说,我会使用Rscript而不是R CMD BATCH

Rscript myscript.R batch.csv

where myscript.R contains: 其中myscript.R包含:

args <- commandArgs(TRUE)
batch_args <- read.table(args[1], sep=",")
# loop over multiple runs

除了使用Rscript(如Josh所说),你还应该使用CRAN包getoptoptparse,因为它们是为了这个目的而编写的。

What do you mean by 'no luck'? “没有运气”是什么意思? The filename is in there, in the commandArgs() function, you just have to work out how to get it out. 文件名在那里,在commandArgs()函数中,你只需要弄清楚如何把它拿出来。 Code and error messages are handy. 代码和错误消息很方便。

That's not a problem if the only extra argument is a filename, you know its position. 如果唯一的额外参数是文件名,那么这不是问题,你知道它的位置。 What will confuse you is when you start having more complex argument passing. 当你开始传递更复杂的参数时,会让你感到困惑的是什么。

You're also complicating things with passing 'fn=foo.csv'. 通过传递'fn = foo.csv'也会使事情复杂化。 Just pass the filename and assign it to fn in your script. 只需传递文件名并将其分配给脚本中的fn即可。 If you really want to use eval you probably need to quote your filename, thus myscript.r is: 如果你真的想使用eval,你可能需要引用你的文件名,因此myscript.r是:

ca = commandArgs(trailingOnly=TRUE)
eval(parse(text=ca))
print(read.csv(fn))

and run thus: 然后运行:

R  --slave "--args fn='batch.csv'" < myscript.r
     A B C
   1 1 2 3
   2 6 8 3

Where batch.csv is a simple csv file. batch.csv是一个简单的csv文件。

You could do a loop over "ca" in your script and eval everything. 您可以在脚本中对“ca”进行循环并对所有内容进行评估。 It's slightly dangerous though, since you could easily break basic functionality. 但这有点危险,因为你很容易打破基本功能。

Personally I'd loop over ca, look for name=value pairs for a known set of names, and set them. 就个人而言,我会遍历ca,为一组已知名称寻找名称=值对,然后设置它们。 Basically implementing getopt, but someone has probably done that already... 基本上实现了getopt,但有人可能已经这样做了......

尝试

fn="batch.csv"; R CMD BATCH --slave "--args $fn" myscript.r output.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM