批处理模式引用参数解析

时间:2011-08-12 14:22:38

标签: r

我正在尝试以批处理模式使用我的R脚本,但R似乎无法正确解析引用的参数:

args=(commandArgs(TRUE))
for(i in 1:length(args)){
   print(paste('ARG ',i,args[[i]],sep=" "))
}

然后,如果提供带空格和引号的参数,例如:

R CMD BATCH "--args foo=2 bar=3 's=string with spaces'"  test-parameters.R output

输出是:

[1] "ARG  1 foo=2"
[1] "ARG  2 bar=3"
[1] "ARG  3 's=string"
[1] "ARG  4 with"
[1] "ARG  5 spaces'"

当然我希望第三个参数是s='string with spaces':有没有办法获得它?

谢谢!

2 个答案:

答案 0 :(得分:1)

是的,R CMD BATCH的行为有点奇怪。

请改为尝试:

R --slave --vanilla --file=test-parameters.R --args foo=2 bar=3 "s=string with spaces" > output

--slave和--vanilla选项可能会根据需要替换为更合适的选项。

答案 1 :(得分:1)

要坚持使用R CMD BATCH,我在需要空格的地方用插入号结束了参数传递,然后执行gsub(“ ^”,“”,argumentPassed,fixed = TRUE)

批处理脚本

R CMD BATCH --no-restore "--args automationRDS='//networkLocation/folder/folder/my^filename^has^spaces.RDS'" "\\networkLocation\folder\folder\RScript.R"

R之内

automationRDS <- gsub("^"," ",automationRDS,fixed=TRUE)

我喜欢R CMD BATCH与Rscript的日志记录功能