I was trying to set a default argument for my S4 method but found it only works if I also set a default value to the generic function. I was hoping someone could explain why this is happening as I would have expected this to work by just providing the default argument to the particular class definition?
myObj <- setClass(Class = "myObj", representation = list(x = "character"))
setGeneric(name = "myMeth", def = function(object, arg1) standardGeneric("myMeth"))
setMethod(
f = "myMeth",
signature = "myObj",
definition = function(object, arg1 = "hello, world") {
print(arg1)
}
)
foo <- myObj()
myMeth(foo)
#> Error in print(arg1) : argument "arg1" is missing, with no default
Compared to:
setGeneric(name = "myMeth", def = function(object, arg1 = "") standardGeneric("myMeth"))
setMethod(
f = "myMeth",
signature = "myObj",
definition = function(object, arg1 = "hello, world") {
print(arg1)
}
)
foo <- myObj()
myMeth(foo)
#> [1] "hello, world"