By changing %dopar% to %do% when using foreach, I can run the code sequentially. How can I do this programmatically?
E.g. I want the following but with only ONE foreach statement:
library(doParallel)
library(foreach)
registerDoParallel(cores = 4)
runner <- function(parallel = FALSE) {
if (parallel)
foreach(i=1:10) %dopar% {
print(i)
}
else
foreach(i=1:10) %do% {
print(i)
}
}
runner()
runner(TRUE)
print(i)
, then maybe convert it to a function, or usesource
. – Impressmentmatch.fun
. – Impressment