I am developing an R package that calls an executable via a system
call. One of the main advantages of using R is I can call it in parallel sessions. When I run this in the R terminal the output from the executable displays live for all parallel processes. When I try this in RStudio it neither shows live, nor at the end of execution, essentially hiding the important output from the user.
Here's a very trivial reprex:
test.fn <- function(i) {Sys.sleep(i); system('where Rterm')}
test.fn(1)
Which prints to the console
C:\Program Files\R\R-4.0.3\bin\x64\Rterm.exe
Now if I try it in parallel:
cores <- 3
library(foreach)
library(doParallel)
registerDoParallel(cores=cores)
test <- foreach(i=1:cores) %dopar% test.fn(i)
No output at all. If I run this same command in the R terminal it works fine.
> test <- foreach(i=1:cores) %dopar% test.fn(i)
C:\Program Files\R\R-4.0.3\bin\x64\Rterm.exe
C:\Program Files\R\R-4.0.3\bin\x64\Rterm.exe
C:\Program Files\R\R-4.0.3\bin\x64\Rterm.exe
I tried this with snowfall
, future
, and the base parallel
packages and all have the same effect. No output in RStudio but works fine in the terminal. It also fails in the Rgui.
Is there any way around this without abandoning RStudio?
Thanks!