Here is a minimal example showing the issue:
mod.r
:
#' @export
run_sqrt <- function (x) {
sqrt(x)
}
mwe.r
box::use(
./mod[...],
parallel,
dp = doParallel,
foreach[foreach, `%dopar%`],
)
cl <- parallel$makeCluster(2L)
dp$registerDoParallel(cl)
foreach(i = 1 : 5) %dopar% {
run_sqrt(i)
}
parallel$stopCluster(cl)
This raises the error
Error in { : task 1 failed - "could not find function "run_sqrt""
I found this
parallel::clusterExport(cluster, setdiff(ls(), "cluster"))
in How to use `foreach` and `%dopar%` with an `R6` class in R?
But it didn't work