I have developed some code which use a lot of local functions and S3 methods. When I have started using S3 methods in furrr functions, autodetection of global variables is not working properly for detection of S3 handler.
Sample code:
test <- function(x) UseMethod("test")
test.character <- function(x) cat("I am character", x)
furrr::future_map(as.character(1:10), test)
It fails with following error:
Error in UseMethod("test") :
no applicable method for 'test' applied to an object of class "character"
I know, that I can use furrr_options(globals = list(test.character = test.character))
, but I need some generic solution, which wouldn't require enumerating in globals all local functions and S3 methods which are used inside furrr functions.
Is there any way to tell furrr how to resolve S3 generic methods?
I don't think it's that common just to define generic S3 methods in your global work environment. Normally that's a thing that happens in packages.
One possible work around is this helper functions which will look for all the S3 methods for your global generics.
get_all_generics <- function() {
funs <- unlist(eapply(globalenv(), FUN = function(x) is.function(x) && utils::isS3stdGeneric(x)))
gfuns <- names(funs[funs])
mget(unlist(lapply(gfuns, methods)), envir=globalenv())
}
get_all_generics()
furrr_options(globals= get_all_generics())