使用system()命令运行多个R脚本 [英] Running multiple R scripts using the system() command

查看:189
本文介绍了使用system()命令运行多个R脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows 7中运行RStudio.我编写了一个主脚本,该主脚本可以生成57个新的R脚本,每个脚本均包含基于两个参数来运行函数的命令:

I'm running RStudio in Windows 7. I have written a master script that generates 57 new R scripts, each with commands to run a function based on two parameters:

vector1 <- c(1:19)
vector2 <- c(1:3)

首先,主脚本使用两个for循环(一个对 vector1 使用索引'abc',一个对 vector2 使用索引'def')生成我的工作目录中的57个脚本中的每个脚本都遵循以下文件名约定:

First, the master script uses two for-loops (one using the index 'abc' for vector1, one using the index 'def' for vector2) to generate each of the 57 scripts in my working directory that take the following filename convention:

run_inference_<<vector1[abc]>>_<<vector2[def]>>.R

该部分成功运行-57个脚本中的每个脚本都是使用正确的命令生成的.我的工作目录现在包含文件 run_inference_1_1.R run_inference_1_2.R 等.

That part runs successfully - each of the 57 scripts is generated with the correct commands inside. My working directory now contains files run_inference_1_1.R, run_inference_1_2.R, etc.

我要做的最后一件事是同时运行我的主人的所有57个脚本.我在for循环中尝试了以下方法:

The final thing I want to do is then run all 57 scripts from my master, and simultaneously. I've tried the following inside the for-loop:

system(paste0("Rscript run_inference_",abc, "_", def, ".R"),wait = F)

这不起作用.但是,如果我打开57个生成的脚本之一并运行它,则可以从该脚本中获得所需的结果.这告诉我问题出在我编写的system()命令中.

This does not work. However, if I open one of the 57 generated scripts and run it then I get the desired result from that script. This tells me the issue is within the system() command that I've written.

这57个脚本中的每一个都不是计算密集型的(到目前为止),我现在要进行的测试应该在PC上花费2分钟.请问如何编辑system()命令以同时执行所有57个脚本?

Each of the 57 scripts will not be computationally intensive (yet), and the test I want to do now should take 2 minutes on my PC. How can I edit my system() command to execute all 57 scripts simultaneously, please?

推荐答案

除非使用正在运行的程序知道如何并行化脚本本身,否则不要通过使用大型脚本调用system来执行此操作.您可以通过从不同的R进程多次调用system来做到这一点.

You don't do this by calling system once with a big script, unless the program you're running knows how to parallelise the script itself. You do this by calling system multiple times from different R processes.

scripts <- paste0("Rscript run_inference_", abc, "_", def, ".R")

# make lots of R processes, assuming the script to be called won't eat CPU
cl <- parallel::makeCluster(30)

parallel::parLapply(cl, scripts, function(script) system(script))
parallel::stopCluster(cl)

这篇关于使用system()命令运行多个R脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆