I am new to S4 OO in R. I am thinking of using class attributes as the default input to common methods, but I got an error message saying that argument "xxx" is missing, with no default.
Basically, I have a list of functions (e.g. class methods) that work with the same set of class attributes. I want to use these class attributes as the default input values ββfor the list of functions, but the way I implemented (I used definition = function(theObject, input = theObject@attribute1), see the code example below) is throwing an error ... Why doesn't this work?
Thank you very much in advance!
class1 = setClass("class1",
slots = c(attribute1 = "numeric"),
prototype = list(
attribute1 = NULL
))
setGeneric(name = "method1",
def = function(theObject, input){
standardGeneric("method1")
})
setMethod(f = "method1",
signature = "class1",
definition = function(theObject, input = theObject@attribute1){
theObject@attribute1 = input + 1
return (theObject)
})
x = class1()
x@attribute1 = 1
method1(x, input = 2)
method1(x)
source
share