2

I believe using system2() is a good option for running two R scripts in parallel. I'm trying something like the following:

Sys.time()
system2(command = 'Sys.sleep(5)', wait = FALSE)
system2('Sys.sleep(7)', wait = FALSE)
Sys.time()

However, it does not work and I'm also getting this warning:

running command '"Sys.sleep(7)"' had status 127

The documentation of system or system2 does not show any example and I can't find much around. Has anyone tried this option to solve this problem?

Dario Federici
  • 1,228
  • 2
  • 18
  • 40

1 Answers1

1

The following works for me:

 system("Rscript -e 'Sys.sleep(5); \"task 1\"'", wait=FALSE)
 system("Rscript -e 'Sys.sleep(7); \"task 2\"'", wait=TRUE)
 [1] "task 1"
 [1] "task 2"

Version with system2() (thanks to the comment of HenrikB):

system2("Rscript", args = c("-e", "'Sys.sleep(5); \"task 1\"'"), wait=FALSE)
system2("Rscript", args = c("-e", "'Sys.sleep(7); \"task 2\"'"), wait=TRUE)
[1] "task 1"
[1] "task 2"
Nairolf
  • 2,418
  • 20
  • 34
  • 2
    The first argument to `system2()` must be an executable, so you want to use `system2("Rscript", args = c("-e", "'Sys.sleep(5); \"task 1\"'"), ...)` in the latter case. – HenrikB Oct 26 '18 at 16:42