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())
}