【问题标题】:Run multiple R scripts concurrently through a batch file通过批处理文件同时运行多个 R 脚本
【发布时间】:2017-10-10 09:40:19
【问题描述】:

我正在尝试按照常规方法通过批处理触发 R 脚本,例如

RScript Example.R

但我期待的是通过批处理文件运行多个 R 脚本的某种方式。

我尝试使用 Start 命令打开多个会话,但这也不起作用。 (RScript START ex1.R START ex2.R)

PS 完成新手到批处理文件。

【问题讨论】:

标签: r batch-file rscript


【解决方案1】:

在 Windows 上 - 如果您想并行运行它们,请确保在 yoyr 批处理 (.bat) 脚本中添加 start。否则Example2.R 等待Example1.R 完成等。

start RScript Example1.R
start RScript Example2.R
...

【讨论】:

    【解决方案2】:

    只需将 RScript 命令保存在 Windows 批处理文件 (.bat) 中,然后双击目录中的文件或通过命令行调用它。下面假设 RScript 是一个环境变量。

    批处理文件(在记事本中输入以下内容并以扩展名.bat保存,而不是默认的.txt)

    cd "C:\Path\To\Scripts"
    
    RScript Example1.R
    RScript Example2.R
    RScript Example3.R
    RScript Example4.R
    RScript Example5.R
    

    CMD 命令行

    call myRScriptBatchFile.bat
    

    PowerShell 命令行

    cmd.exe /c myRScriptBatchFile.bat
    

    【讨论】:

      【解决方案3】:

      如果您使用 sh 启动脚本,这可以做到。

      cd /path_to_script1/
      sh script1.sh &
      cd /path_to_script2/
      sh script2.sh &
      cd /path_to_script3/
      sh scipt2.sh &
      

      这会启动并行 R 会话(每个脚本一个),因此请注意内存和 CPU 使用。每个脚本文件都包含 Rscript 命令。

      【讨论】:

      • 到目前为止,我在 .bat 文件中有语句 RScript Example.R 并执行了 .bat 文件。鉴于我在 Windows 系统上,我可以使用 .sh 扩展名来做到这一点吗?
      • 可能不是最好的,但您可以使用 cygwin 终端在 windows 中运行 sh 文件