Justin Landis
Justin Landis

Reputation: 2071

R: S3 Method dispatch depending on arguments

I have a generic function foo that I want to call three different ways depending on the arguments given to it.

foo <- function(...) UseMethod("foo")

#default
foo.default <- function(x, y, ...) {
#does some magic
print("this is the default method")
}

#formula
foo.formula <- function(formula, data = list(), ...) {
print("this is the formula method")
}

#data.frame
foo.data.frame <- function(data, x, y, ...) {
print("this is the data.frame method")
}

In the following I'm going to show how I am expecting the method dispatch to work but the outputs are presented under each call...

mydata <- data.frame(x=c(1,2,3,4),y=c(5,6,7,8))

#ways to call default function
foo(x = mydata$x, y = mydata$y)
#[1] "this is the default method"

#ways to call formula
foo(formula = mydata$x~mydata$y)
#[1] "this is the formula method"
foo(formula = x~y, data = mydata)
#[1] "this is the formula method"
foo(data = mydata, formula = x~y)  #ERROR
#[1] "this is the data.frame method"

#ways to call data.frame method
foo(data = mydata, x = x, y = y)
#[1] "this is the data.frame method"
foo(x = x, y = y, data = mydata) #ERROR
#Error in foo(x = x, y = y, data = mydata) : object 'x' not found

from what I can tell, the method used depends on the class of the first argument. Essentially, I would like for the method dispatch to depend on the arguments passed to the generic function foo and not the first argument.

I would like the dispatch to have the following priority:

If the formula argument is present the formula method is used (data argument should be optional here)

Then, if no formula argument is found, if data argument is present use data.frame method (which requires x and y arguments)

else foo expects the x and y arguments or it will fail.

Note

I would like to avoid defining the generic function foo as follows

foo <- function(formula, data,...) UseMethod("foo")

while this would fix all my issues (I believe all except the last case), this will cause a devtools::check() warning because the some of S3 functions will not have the same arguments as the generic function and will no longer be consistent (specifically foo.default and foo.data.frame). And I wouldn't like to include the missing arguments because those methods do not have use for those arguments.

Upvotes: 3

Views: 1097

Answers (1)

coffeinjunky
coffeinjunky

Reputation: 11514

As Thomas has pointed out, this is not the standard behavior for S3 classes. If you really want to stick to S3, however, you could write your functions so as to "mimick" UseMethod, even though it won't be pretty and is probably not what you want to do. Nevertheless, here an idea that is based on capturing all arguments first, and then checking for the presence of your "preferred" argument type:

Get some objects first:

a <- 1; class(a) <- "Americano"
b <- 2; class(b) <- "Espresso"

Let the function in question capture all arguments with dots, and then check for the presence of an argument type in order of your preference:

drink <- function(...){
  dots <- list(...)

  if(any(sapply(dots, function(cup) class(cup)=="Americano"))){
    drink.Americano(...)
    } else { # you can add more checks here to get a hierarchy
        # try to find appropriate method first if one exists, 
        # using the first element of the arguments as usual
        tryCatch(get(paste0("drink.", class(dots[[1]])))(), 
        # if no appropriate method is found, try the default method:
             error = function(e) drink.default(...)) 
  }
}

drink.Americano <- function(...) print("Hmm, gimme more!")
drink.Espresso <- function(...) print("Tripple, please!")
drink.default <- function(...) print("Any caffeine in there?")

drink(a) # "Americano", dispatch hard-coded.
# [1] "Hmm, gimme more!"
drink(b) # "Espresso", not hard-coded, but correct dispatch anyway
# [1] "Tripple, please!"
drink("sthelse") # Dispatches to default method
# [1] "Any caffeine in there?"
drink(a,b,"c")
# [1] "Hmm, gimme more!"
drink(b,"c", a)
# [1] "Hmm, gimme more!"

Upvotes: 2

Related Questions