使用 Start 命令让 Windows 以批处理模式启动 R
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1188544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting windows to start R in batch mode using the Start command
提问by JD Long
I know I must be making a simple syntax mistake, but I want to have a windows batch file that fires up 9 instances of R and runs a different routine in each one. I want these to run simultaneously (i.e. asynchronously). I can fire up 9 command prompt windows and type a command in each one, but it seems like with the START command I should be able to make them start from a single batch file.
我知道我一定是犯了一个简单的语法错误,但我想要一个 Windows 批处理文件来启动 9 个 R 实例并在每个实例中运行不同的例程。我希望这些同时运行(即异步)。我可以启动 9 个命令提示符窗口并在每个窗口中键入一个命令,但似乎使用 START 命令我应该能够使它们从单个批处理文件启动。
Here's an example of how I start one of the instances of R:
这是我如何启动 R 实例之一的示例:
"C:\Program Files (x86)\R\R-2.8.1\bin\R" CMD BATCH "C:\Users\jd\Documents\mexico\Estado\getdata1.r"
Reading this previous stackoverflow questionalong with this previous questionmakes me think I should be able to do this:
阅读上一个 stackoverflow 问题和上一个问题让我觉得我应该能够做到这一点:
START "" "C:\Program Files (x86)\R\R-2.8.1\bin\R" CMD BATCH "C:\Users\jd\Documents\mexico\Estado\getdata1.r" /b
That does not return an error, it just returns a prompt and R never starts. What am I missing?
这不会返回错误,它只会返回一个提示并且 R 永远不会启动。我错过了什么?
回答by Dirk Eddelbuettel
I would do two things differently:
我会以不同的方式做两件事:
Use R itself to dispatch nine different jobs; the snowpackage is very good at this even when do not use MPI / PVM / NWS for distributed work. Some examples for snow use are for example in my 'introduction to high performance computing with R' tutorials linked from this page. With snow, you get 'parallel' versions of the apply functions that you can run over multiple instances of R running on the local computer (or of course a network of computers if have one). The r-sig-hpc list is helpful for more detailed questions.
Switch to using Rscript.exe instead of using 'R CMD BATCH'. On Linux / OS X you also get a choice of using littler
使用 R 本身来调度九个不同的作业;即使不使用 MPI/PVM/NWS 进行分布式工作,snow包也非常擅长这一点。雪使用的一些实例例如在我的介绍到高性能有R计算'从链接教程此页面。使用snow,您可以获得应用函数的“并行”版本,您可以在本地计算机(当然也可以是计算机网络,如果有的话)上运行的多个R实例上运行这些函数。r-sig-hpc 列表有助于解决更详细的问题。
切换到使用 Rscript.exe 而不是使用“R CMD BATCH”。在 Linux / OS X 上,您还可以选择使用 littler
That said, I run almost all my jobs on Linux so there may be a Windows-specific answer here too that I just do not know. But the above is generic and stays in the platform-agnostic spirit of R.
也就是说,我几乎所有的工作都在 Linux 上运行,所以这里也可能有一个我不知道的特定于 Windows 的答案。但上述内容是通用的,并且保持了 R 的平台无关精神。
回答by Andrew Redd
Simple answer. On windows when running command use "Rcmd" not "R CMD". There is a separate exe for running the commands. Look in the bin folder of your R installation.
简单的回答。在 Windows 上运行命令时使用“Rcmd”而不是“R CMD”。有一个单独的 exe 用于运行命令。查看 R 安装的 bin 文件夹。
回答by Lucas Fortini
It was not immediately clear from the other answers how to actually make this work (without resorting to parallel processing alternatives, so here is a solution I found that works very simply on windows
从其他答案中并没有立即清楚如何实际进行这项工作(不诉诸并行处理替代方案,所以这是我发现的一个解决方案,它在 Windows 上非常简单地工作
If you have a simple r file:
如果您有一个简单的 r 文件:
for(i in 1:10){
ptm0 <- proc.time()
Sys.sleep(0.5)
ptm1=proc.time() - ptm0
jnk=as.numeric(ptm1[3])
cat('\n','It took ', jnk, "seconds to do iteration", i)
}
On CMD, specify the directory of where your script is and then start a new window with Rscript to run your code. Multiple lines will open multiple r instances that run your code that also reproduce the messages that the code outputs.
在 CMD 上,指定您的脚本所在的目录,然后使用 Rscript 启动一个新窗口来运行您的代码。多行将打开多个运行您的代码的 r 实例,这些实例也会重现代码输出的消息。
cd "C:\rcode"
START "" Rscript example_code.r /b
START "" Rscript example_code.r /b
If Rscript is not on the system path, just specify the full path instead:
如果 Rscript 不在系统路径上,只需指定完整路径:
START "" "C:\Program Files\R\bin\x64\Rscript.exe" text_within_loop.r /b