1

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?

vkolesnik
  • 367
  • 2
  • 13

1 Answers1

1

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())
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • I agree with you about that it is bad idea to work with local S3 methods, but at the moment I have to go with local S3 in order to fast check that my solution is working. I will let you once I try your workaround. Thanks. – vkolesnik Mar 15 '21 at 08:45
  • I was also able to go on with futures with simply: `furrr::furrr_options(globals = names(globalenv()))`, however this is quite expensive operation from the point of view of big environments. – vkolesnik Apr 22 '21 at 13:16