S4:使用 class 属性作为 class 方法的默认输入值
S4: Use class attributes as default input values for class methods
我是 R 中 S4 OO 系统的新手。我想使用 class 属性作为泛型方法的默认输入,但我收到一条错误消息,指出 argument "xxx" is missing, with no default
。
基本上我有一个函数列表(即 class 方法),它们在同一组 class 属性上运行。我希望使用这些 class 属性作为函数列表的默认输入值,但是我实现的方式(我使用 definition = function(theObject, input = theObject@attribute1)
,请参见下面的示例代码)会抛出错误...为什么它不起作用?
非常感谢!
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() # instantiate an object from class1
x@attribute1 = 1 # set attribute value
method1(x, input = 2) # This works fine if I use "input = 2" explicitly
# This throws the following error "Error in method1(x) : argument "input" is missing, with no default"
# Even though in setMethod part I did specify the default value of input
method1(x)
不确定为什么会这样,但为了使用 setMethod
为输入设置默认值,泛型也需要默认值。例如:
class1 = setClass("class1",
slots = c(attribute1 = "numeric"),
prototype = list(
attribute1 = NULL
))
setGeneric(name = "method1",
def = function(theObject, input=NA){ #Setting any input here enables defaults for methods
standardGeneric("method1")
})
setMethod(f = "method1",
signature = "class1",
definition = function(theObject, input=1){
theObject@attribute1 = input + 1
return (theObject)
})
x = class1()
x@attribute1 = 1
method1(x, input = 2)
method1(x)
我是 R 中 S4 OO 系统的新手。我想使用 class 属性作为泛型方法的默认输入,但我收到一条错误消息,指出 argument "xxx" is missing, with no default
。
基本上我有一个函数列表(即 class 方法),它们在同一组 class 属性上运行。我希望使用这些 class 属性作为函数列表的默认输入值,但是我实现的方式(我使用 definition = function(theObject, input = theObject@attribute1)
,请参见下面的示例代码)会抛出错误...为什么它不起作用?
非常感谢!
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() # instantiate an object from class1
x@attribute1 = 1 # set attribute value
method1(x, input = 2) # This works fine if I use "input = 2" explicitly
# This throws the following error "Error in method1(x) : argument "input" is missing, with no default"
# Even though in setMethod part I did specify the default value of input
method1(x)
不确定为什么会这样,但为了使用 setMethod
为输入设置默认值,泛型也需要默认值。例如:
class1 = setClass("class1",
slots = c(attribute1 = "numeric"),
prototype = list(
attribute1 = NULL
))
setGeneric(name = "method1",
def = function(theObject, input=NA){ #Setting any input here enables defaults for methods
standardGeneric("method1")
})
setMethod(f = "method1",
signature = "class1",
definition = function(theObject, input=1){
theObject@attribute1 = input + 1
return (theObject)
})
x = class1()
x@attribute1 = 1
method1(x, input = 2)
method1(x)