Skip to content

Commit c661d1b

Browse files
Jiefei-Wangmtmorgan
authored andcommitted
Fix the lazy loading issue that prevents package from loading in workers
1 parent e320087 commit c661d1b

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

R/utilities.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,26 @@
129129
timeout <- Inf
130130
timeout
131131
}
132+
133+
## This function walks through a nested list and attempts to trigger
134+
## loading of any encountered S4 class definitions using getClassDef().
135+
##
136+
## Limitations:
137+
## - It does NOT guarantee that all required packages will be loaded,
138+
## because S4 objects may be hidden in attributes, environments, or other
139+
## non-list structures that are not traversed here.
140+
.autoload_s4_classes <- function(obj) {
141+
seen <- character()
142+
recurse <- function(x) {
143+
if (isS4(x)) {
144+
cl <- class(x)
145+
if (!cl %in% seen) {
146+
seen <<- c(seen, cl)
147+
methods::getClassDef(cl)
148+
}
149+
} else if (is.list(x)) {
150+
lapply(x, recurse)
151+
}
152+
}
153+
recurse(obj)
154+
}

R/worker.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@
311311

312312
t1 <- proc.time()
313313
value <- tryCatch({
314+
## Lazy loading causes some packages fail to load in the worker
315+
## environment. This works in 99% cases (see function comments).
316+
.autoload_s4_classes(msg$data$args$X)
314317
do.call(msg$data$fun, msg$data$args)
315318
}, error=function(e) {
316319
## return as 'list()' because msg$fun has lapply semantics

0 commit comments

Comments
 (0)