我目前正在开发一个包,我们称之为myPack。 我有一个名为myFunc1的函数和另一个名为myFunc2的函数,大致如下:
myFunc2 <- function(x, parallel = FALSE) {
if(parallel) future::plan(future::multiprocess)
values <- furrr::future_map(x, myFunc1)
values
}
如果我在非并行状态下调用myFunc2,它可以工作。但是如果我将parallel = TRUE作为参数调用它,则会出现以下错误:
Error: Unexpected result (of class ‘snow-try-error’ != ‘FutureResult’)
retrieved for MultisessionFuture future (label = ‘<none>’, expression =
‘{; do.call(function(...) {; ...future.f.env <- environment(...future.f);
if (!is.null(...future.f.env$`~`)) {; if
(is_bad_rlang_tilde(...future.f.env$`~`)) {; ...future.f.env$`~` <-
base::`~`; ...; }); }, args = future.call.arguments); }’): there is no
package called 'myPack'. This suggests that the communication with
MultisessionFuture worker (‘SOCKnode’ #1) is out of sync.
有人知道为什么myFunc2在顺序模式下运行正常,但并行模式下会出错,我该如何防止这个错误的出现吗?
可重现的示例:
linear_model_1 <- lm(mpg ~ cyl + disp + hp, data = mtcars)
linear_model_2 <- lm(mpg ~ cyl + poly(disp, 2), data = mtcars)
x <- list(linear_model_1, linear_model_2)
myFunc1 <- function(model, seed, size) {
`%>%` <- purrr::`%>%`
set.seed(seed)
draws <- rep(size, 10) %>%
furrr::future_map(sample, x = fitted(model), replace = TRUE)
mean(unlist(draws))
}
(注:这不是整个函数,但基本上可以用缩短版来解释)
(Note: 这不是整个函数,但基本上可以用缩短版来解释)
x
和myFunc1
,以便重现您的问题。 - F. Privéseed
和size
没有在任何地方声明。 - HenrikB