Skip to Content

S4 missing or NULL arguments to methods?

Is there a way to define what happens when the argument of a method is missing or NULL?

In the example below, I'd like to call the same function no matter whether I type foo() or foo(NULL). Of course I know that I can have a method setMethod("foo","NULL",function(x) foo()) but that that's code replication and a possible source of mistakes.


setGeneric("foo",function(x) standardGeneric("foo"))
setMethod("foo","numeric",function(x) "numeric")
setMethod("foo","NULL",function(x) "NULL")
setMethod("foo","missing",function(x) "missing")
R> foo(1)
[1] "numeric"
R> foo()
[1] "missing"
R> foo(NULL)
[1] "NULL"