2

I wrote a program to_source.R which leaves some configuration parameters out. Now I want to run for different parameters in parallel. For example,

In to_source.R

cat(paste0(config, ' is finished')

and in main.R

config = 1
source("to_source.R")

This works. But running in parallel (with foreach/doParallel in windows)

In main_para.R

parallel_cluster = parallel::makeCluster(5)
doParallel::registerDoParallel(parallel_cluster)

configs = 6:10
foreach(config = configs, .combine = rbind, .packages = c("data.table", "foreach")) %dopar% {
  source("to_source.R")
}

parallel::stopCluster(parallel_cluster)

This fails because Error in { : task 1 failed - object 'config' not found

This is very close to the question How to pass command-line arguments when source() an R file, but the answer there does not work under foreach

jf328
  • 6,841
  • 10
  • 58
  • 82

0 Answers0