uses a function that calls another in a foreach loop dopar

Hello,

I have two functions myfunct1 and myfunct2. myfunct1 uses the myfunct2 function I want to use these two functions in a foreach()%dopar%{} loop but i get a error:

Error in {: task 1 failed - "unable to find function 'myfunct2'"

I create util and funct files with this commands:

golem::add_fct("function", with_test = F)
golem::add_utils("util", with_test = F)

utils.util.R

myfunct1 <- function(){
  return(myfunct2())
}

myfunct2 <- function(){
  return(matrix(c("1", "2"), nrow = 1))
}

ftc_function.R

#' @importFrom parallel makeCluster
#' @importFrom parallel stopCluster
#' @importFrom parallel detectCores
#' @importFrom doParallel registerDoParallel
#' @importFrom foreach %dopar%
#' @importFrom foreach foreach
rbinddf <- function(){
  num_cores <- detectCores()
  cl <- makeCluster(num_cores)
  print(getwd())
  registerDoParallel(cl)
  df <- foreach(i = 1:5, .combine = 'rbind',
                .packages = c(),
                .export = c("myfunct1", "myfunct2")) %dopar% {
                  return(myfunct1())
                }
  stopCluster(cl)
  env <- foreach:::.foreachGlobals
  rm(list=ls(name=env), pos=env)
  return(df)
}

app_server.R

app_server <- function(input, output, session) {
  print(rbinddf())
}

must be a golem issue; I never used golem though :frowning:

Hello,
Solutions here: [BUG] a function is not found in the env when I use the parallel package · Issue #1130 · ThinkR-open/golem · GitHub

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.