如何为继承的插槽定义S4原型 [英] How to define an S4 prototype for inherited slots
问题描述
我有一个基类(我们称其为"A"),其表示对于许多其他类是通用的.
I have a base class (let's call it "A") whose representation is common to many other classes.
因此,我定义了其他类(例如"B")来包含该类.
Therefore I define other classes, such as "B", to contain this class.
我想将这些其他类(B)的原型设置为包括从A继承的插槽的默认值.我认为这很自然:
I would like to set the prototype of these other classes (B) to include the default values for the slots inherited from A. I thought this would be natural:
setClass("A", representation(a="character"))
setClass("B", contains="A", prototype(a = "hello"))
但是会产生错误:
Error in representation[!slots] : object of type 'S4' is not subsettable
不确定为什么会这样.如果我省略了原型,我可以做:
Not sure why this happens. If I omit the prototype I can do:
setClass("B", contains="A")
然后破解我自己的生成器函数:
and then hack my own generator function:
new_B <- function(...){
obj <- new("B", ...)
obj@a = "hello"
obj
}
,然后使用new_B()
基于原型创建我的对象,但是与使用通用生成器new("B")
并拥有我的原型相比,这是非常粗陋和丑陋的.
and then create my object based on the prototype with new_B()
, but that's terribly crude and ugly compared to using the generic generator new("B")
and having my prototype...
推荐答案
您只需要命名参数即可:
You just need to name the argument:
setClass("A", representation(a="character"))
setClass("B", contains="A", prototype=prototype(a="hello"))
这篇关于如何为继承的插槽定义S4原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!