Internal function of R package not found when usin

2019-09-02 19:26发布

问题:

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?

标签: r r-future