私は現在パッケージに取り組んでいます。それが 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))
}
(注: これは関数全体ではありませんが、基本的に短縮版で行うことです)