r - 使用带有检查点的 foreach 时未找到 R 包的内部函数
问题描述
这是这个问题的后续问题:How to set .libPaths (checkpoint) on workers when running parallel computing in R
根据答案,我将以下代码(简化示例)放入名为“test1”的 R 包中:
#' @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)
}
然后我调用checkpoint::checkpoint("2018-07-24")
并将包安装到检查点库中。然后下面的代码产生了这个错误:
checkpoint::checkpoint("2018-07-24")
future::plan("multisession")
test1::f()
工作人员使用的初始库路径(PID 16880):-'C:/Programme/R-3.5.1/library'
工作人员使用的库路径(PID 16880):-'.../.checkpoint/2018-07-24/lib/x86_64-w64-mingw32/3.5.1'
工作人员使用的初始库路径(PID 3916):-'C:/Programme/R-3.5.1/library'
工作人员使用的库路径(PID 3916):-'.../.checkpoint/2018-07-24/lib/x86_64-w64-mingw32/3.5.1'
{ 中的错误:任务 1 失败 - “找不到函数“h””
test1:::g()
当我在通话中明确调用foreach
时,它可以工作。有人可以解释一下,为什么我必须在 foreach 循环中显式调用我自己的包的函数:::
?看起来g()
好像找到了,但h()
后来没有?
解决方案
推荐阅读
- ios - 关于重新打开应用程序时API流程的问题
- c - 为什么 printf 打印我分配给 int 空间的空格数?
- typescript - 我们应该在转换通用 json 响应时使用“any”吗
- ios - Apple 硅 DTK && Xcode12.2 rc && arm64 ios 模拟器 && 为 ios sim 构建,但链接 ios
- javascript - 为什么在 div 上有多次点击,使用 jQuery 3.5.1
- python - 如何使用beautifulSoup从该站点抓取文本
- node.js - 用于 Node/NPM 的预定义、专用系统环境变量
- c++builder - 将焦点设置到 TPrintDialog 中的 OK 按钮
- php - 如何从 php 中的 echo 内的按钮调用函数?
- sql - 寻找不到 10 年的员工