Internal function of R package not found when using foreach with checkpoint

旧巷老猫 提交于 2019-12-11 19:06:07

问题


This is a follow-up question of this question: How to set .libPaths (checkpoint) on workers when running parallel computation in R

Based on the answer I put the following code (simplified example) into an R package called 'test1':

#' @export
f <- function() {
  `%dopar%` <- foreach::`%dopar%`
  doFuture::registerDoFuture()
  libs <- .libPaths()

  res <- foreach::foreach(x = 1:2) %dopar% {

    cat(sprintf("Initial Library paths used by worker (PID %d):\n", Sys.getpid()))
    cat(sprintf(" - %s\n", sQuote(.libPaths()[1])))

    ## Use the same library paths as the master R session
    .libPaths(libs)

    cat(sprintf("Library paths used by worker (PID %d):\n", Sys.getpid()))
    cat(sprintf(" - %s\n", sQuote(.libPaths()[1])))

    x + g()
  }

  res
}


g <- function() {
  h()
}

h <- function() {
  runif(1)
}

Then I called checkpoint::checkpoint("2018-07-24") and installed the package into the checkpoint library. The following code then produced this error:

checkpoint::checkpoint("2018-07-24")
future::plan("multisession")
test1::f()

Initial Library paths used by worker (PID 16880): - 'C:/Programme/R-3.5.1/library'

Library paths used by worker (PID 16880): - '.../.checkpoint/2018-07-24/lib/x86_64-w64-mingw32/3.5.1'

Initial Library paths used by worker (PID 3916): - 'C:/Programme/R-3.5.1/library'

Library paths used by worker (PID 3916): - '.../.checkpoint/2018-07-24/lib/x86_64-w64-mingw32/3.5.1'

Error in { : task 1 failed - "could not find function "h""

When I explicitly call test1:::g() inside the foreach call, it works though. Can someone explain, why I have to explicitly call a function of my own package inside a foreach loop with :::? It looks like g() seems to be found, but h() then not?

来源:https://stackoverflow.com/questions/53117396/internal-function-of-r-package-not-found-when-using-foreach-with-checkpoint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!