Skip to content
Prev 389001 / 398506 Next

What if there's nothing to dispatch on?

On 01/09/2021 6:29 p.m., Rolf Turner wrote:
I don't know the header of your foo() method, but let's suppose foo() is

   foo <- function(x, data, ...) {
     UseMethod("foo")
   }

with

   foo.formula <- function(x, data, ...) {
     # do something with the formula x
   }

   foo.default <- function(x, data, ...) {
     # do the default thing.
   }

Now you have

   xxx <- data.frame(u = 1:10, v = rnorm(10))
   foo(x = u, y = v, data = xxx)

You want this to dispatch to the default method, because u is not a 
formula, it's a column in xxx.  But how do you know that?  Maybe in some 
other part of your code you have

   u <- someresponse ~ somepredictor

So now u *is* a formula, and this will dispatch to the formula method, 
causing havoc.

I think Bill's suggestion doesn't help here.  To do what you want to do 
doesn't really match what S3 is designed to do.

Duncan