我想使用批处理文件运行许多文件,该批处理文件将使用for循环从R执行。例如,假设我有2次要执行的运行:
runs <- c(102, 103)系统命令的语法要求首先指定批处理文件,然后是运行的输入数据文件(102.txt和103.txt),以及执行批处理文件后输出结果文件的名称(102.res和103.res)。我试图使用for循环来运行它:
for (r in runs) {
cmd <- sprintf('C:/example1/test.bat %d.txt %d.res', runs, runs)[1]
print(eval(cmd))
command: system(cmd)
}
[1] "C:/example1/test.bat 102.txt 102.res"不幸的是,这只执行第一次运行(102),而不进入下一次运行(103)。R控制台显示以下警告:
Error in command:system(cmd) : NA/NaN argument考虑到这个错误是阻止R前进到下一次运行的原因,我尝试在for循环中使用options(warn = -1):
for (r in runs) {
options(warn = -1)
cmd <- sprintf('C:/example1/test.bat %d.ctl %d.res', runs, runs)[1]
print(eval(cmd))
command: system(cmd)
options(warn = 0)
}不幸的是,这继续引发同样的错误。至于它的价值,我的批处理文件(102.res)的输出正是我所希望的那样,我只是希望能够绕过这个错误,继续我的其余的运行。对如何做好这件事有什么想法吗?
提前谢谢。
发布于 2014-10-19 21:41:55
这是你所拥有的
runs <- c(102, 103)
for (r in runs) {
cmd <- sprintf('C:/example1/test.bat %d.txt %d.res', runs, runs)[1]
print(eval(cmd))
# command: system(cmd)
}哪种输出
[1] "C:/example1/test.bat 102.txt 102.res"
[1] "C:/example1/test.bat 102.txt 102.res"尝试使用循环变量r代替数组运行,在cmd <-.线路
for (r in runs) {
cmd <- sprintf('C:/example1/test.bat %d.txt %d.res', r, r)[1] # <- change runs to r
print(eval(cmd))
# command: system(cmd)
}输出是
[1] "C:/example1/test.bat 102.txt 102.res"
[1] "C:/example1/test.bat 103.txt 103.res"https://stackoverflow.com/questions/26455510
复制相似问题