Skip to content

[BUG] a function is not found in the env when I use the parallel package #1130

Open
@DevIA05

Description

@DevIA05

Describe the bug
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'"

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

session info

> devtools::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.3 (2023-03-15 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  French_France.utf8
 ctype    French_France.utf8
 tz       Indian/Mauritius
 date     2024-02-22
 rstudio  2023.03.0+386 Cherry Blossom (desktop)
 pandoc   NAPackages ────────────────────────────────────────────────────────────────────────────
 ! package        * version    date (UTC) lib source
   attachment       0.4.0      2023-05-31 [1] CRAN (R 4.2.3)
   attempt          0.3.1      2020-05-03 [1] CRAN (R 4.2.3)
   brio             1.1.3      2021-11-30 [1] CRAN (R 4.2.3)
   bslib            0.5.1      2023-08-11 [1] CRAN (R 4.2.3)
   cachem           1.0.8      2023-05-01 [1] CRAN (R 4.2.3)
   callr            3.7.3      2022-11-02 [1] CRAN (R 4.2.3)
   cli              3.6.1      2023-03-23 [1] CRAN (R 4.2.3)
   codetools        0.2-19     2023-02-01 [2] CRAN (R 4.2.3)
   colorspace       2.1-0      2023-01-23 [1] CRAN (R 4.2.3)
   config           0.3.2      2023-08-30 [1] CRAN (R 4.2.3)
   crayon           1.5.2      2022-09-29 [1] CRAN (R 4.2.3)
   desc             1.4.2      2022-09-08 [1] CRAN (R 4.2.3)
   devtools         2.4.5      2022-10-11 [1] CRAN (R 4.2.3)
   digest           0.6.33     2023-07-07 [1] CRAN (R 4.2.3)
   doParallel       1.0.17     2022-02-07 [1] CRAN (R 4.2.3)
   dplyr            1.1.2      2023-04-20 [1] CRAN (R 4.2.3)
   DT               0.31       2023-12-09 [1] CRAN (R 4.2.3)
   ellipsis         0.3.2      2021-04-29 [1] CRAN (R 4.2.3)
   fansi            1.0.4      2023-01-22 [1] CRAN (R 4.2.3)
   fastmap          1.1.1      2023-02-24 [1] CRAN (R 4.2.3)
   fontawesome      0.5.2      2023-08-19 [1] CRAN (R 4.2.3)
   foreach          1.5.2      2022-02-02 [1] CRAN (R 4.2.3)
   fs               1.6.3      2023-07-20 [1] CRAN (R 4.2.3)
   generics         0.1.3      2022-07-05 [1] CRAN (R 4.2.3)
   ggplot2          3.4.4      2023-10-12 [1] CRAN (R 4.2.3)
   glue             1.6.2      2022-02-24 [1] CRAN (R 4.2.3)
   golem            0.4.1      2023-06-05 [1] CRAN (R 4.2.3)
   gtable           0.3.4      2023-08-21 [1] CRAN (R 4.2.3)
   htmltools        0.5.6      2023-08-10 [1] CRAN (R 4.2.3)
   htmlwidgets      1.6.2      2023-03-17 [1] CRAN (R 4.2.3)
   httpuv           1.6.11     2023-05-11 [1] CRAN (R 4.2.3)
   iterators        1.0.14     2022-02-05 [1] CRAN (R 4.2.3)
   jquerylib        0.1.4      2021-04-26 [1] CRAN (R 4.2.3)
   jsonlite         1.8.4      2022-12-06 [1] CRAN (R 4.2.3)
   knitr            1.44       2023-09-11 [1] CRAN (R 4.2.3)
   later            1.3.1      2023-05-02 [1] CRAN (R 4.2.3)
   lifecycle        1.0.3      2022-10-07 [1] CRAN (R 4.2.3)
   magrittr         2.0.3      2022-03-30 [1] CRAN (R 4.2.3)
   memoise          2.0.1      2021-11-26 [1] CRAN (R 4.2.3)
   mime             0.12       2021-09-28 [1] CRAN (R 4.2.0)
   miniUI           0.1.1.1    2018-05-18 [1] CRAN (R 4.2.3)
   munsell          0.5.0      2018-06-12 [1] CRAN (R 4.2.3)
   pillar           1.9.0      2023-03-22 [1] CRAN (R 4.2.3)
   pkgbuild         1.4.2      2023-06-26 [1] CRAN (R 4.2.3)
   pkgconfig        2.0.3      2019-09-22 [1] CRAN (R 4.2.3)
   pkgload          1.3.3      2023-09-22 [1] CRAN (R 4.2.3)
   prettyunits      1.2.0      2023-09-24 [1] CRAN (R 4.2.3)
   processx         3.8.2      2023-06-30 [1] CRAN (R 4.2.3)
   profvis          0.3.8      2023-05-02 [1] CRAN (R 4.2.3)
   promises         1.2.1      2023-08-10 [1] CRAN (R 4.2.3)
   ps               1.7.5      2023-04-18 [1] CRAN (R 4.2.3)
   purrr            1.0.2      2023-08-10 [1] CRAN (R 4.2.3)
   R6               2.5.1      2021-08-19 [1] CRAN (R 4.2.3)
   Rcpp             1.0.10     2023-01-22 [1] CRAN (R 4.2.3)
   remotes          2.4.2.1    2023-07-18 [1] CRAN (R 4.2.3)
   rlang            1.1.1      2023-04-28 [1] CRAN (R 4.2.3)
   roxygen2         7.2.3      2022-12-08 [1] CRAN (R 4.2.3)
   rprojroot        2.0.3      2022-04-02 [1] CRAN (R 4.2.3)
   rsconnect        1.1.0      2023-09-05 [1] CRAN (R 4.2.3)
   rstudioapi       0.15.0     2023-07-07 [1] CRAN (R 4.2.3)
   sass             0.4.7      2023-07-15 [1] CRAN (R 4.2.3)
   scales           1.3.0      2023-11-28 [1] CRAN (R 4.2.3)
   sessioninfo      1.2.2      2021-12-06 [1] CRAN (R 4.2.3)
   shiny          * 1.7.5      2023-08-12 [1] CRAN (R 4.2.3)
   shinydashboard   0.7.2      2021-09-30 [1] CRAN (R 4.2.3)
   shinymaterial    1.2.0      2020-08-31 [1] CRAN (R 4.2.3)
   stringi          1.7.12     2023-01-11 [1] CRAN (R 4.2.2)
   stringr          1.5.0      2022-12-02 [1] CRAN (R 4.2.3)
 R testgolem      * 0.0.0.9000 <NA>       [?] <NA>
   testthat         3.1.10     2023-07-06 [1] CRAN (R 4.2.3)
   tibble           3.2.1      2023-03-20 [1] CRAN (R 4.2.3)
   tidyselect       1.2.0      2022-10-10 [1] CRAN (R 4.2.3)
   urlchecker       1.0.1      2021-11-30 [1] CRAN (R 4.2.3)
   usethis          2.2.2      2023-07-06 [1] CRAN (R 4.2.3)
   utf8             1.2.3      2023-01-31 [1] CRAN (R 4.2.3)
   vctrs            0.6.3      2023-06-14 [1] CRAN (R 4.2.3)
   withr            2.5.0      2022-03-03 [1] CRAN (R 4.2.3)
   xfun             0.40       2023-08-09 [1] CRAN (R 4.2.3)
   xml2             1.3.3      2021-11-30 [1] CRAN (R 4.2.3)
   xtable           1.8-4      2019-04-21 [1] CRAN (R 4.2.3)
   yaml             2.3.7      2023-01-23 [1] CRAN (R 4.2.3)

 [1] C:/Users/user/AppData/Local/R/win-library/4.2
 [2] C:/Program Files/R/R-4.2.3/library

 R ── Package was removed from disk.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions