好的,我花了一段时间来创建一段代码来复制我的问题。这里是。请注意,如果您运行该命令new("FirstSet", id = "Input", multiplier = 2)
,您将得到正确的答案。但是,如果您尝试创建一个包含两者的类,您将获得以下信息:Error in .local(.Object, ...) : argument "id" is missing, with no default
. 这实际上是我能做的最好的解释/显示问题。
我到底做错了什么?
setClass("Details",
representation(
ID = "character",
Anumber = "numeric"))
Input <- new("Details",
ID = "Input",
Anumber = 2)
setClass("FirstSet",
representation(
Anothernumber = "numeric"))
setGeneric(
name = "FirstSet",
def = function(object){standardGeneric("FirstSet")}
)
setMethod("initialize",
signature(.Object = "FirstSet"),
function (.Object, id, multiplier)
{ x = id@Anumber
y = x * multiplier
.Object@Anothernumber = y
return(.Object)
}
)
setClass("Super", contains = c("Details", "FirstSet"))
更正后的代码现在给出了一个新错误。我按照帖子中的说明解决了我的问题。我还为“Super”创建了一个泛型和一个方法,请参见下面的代码。现在,我得到一个新的错误。.local(.Object, ...) 中的错误:试图从没有插槽的基本类(“字符”)的对象中获取插槽“Anumber”。伙计,这很累,我以为我有。
细节的目标是,将有许多文件被序列化,并且根据文件中数据的特征调用方法。这在 R 中是否可能,或者我是否正在尝试做 R 不能做的事情?
新代码
setClass("Details",
representation(
ID = "character",
Anumber = "numeric"))
setGeneric("Details",
def = function(object){standardGeneric("Details")})
setMethod("initialize",
signature(.Object = "Details"),
function(.Object, ID = character(), Anumber = numeric()){
.Object@ID = ID
.Object@Anumber = 2
return(.Object)
})
setClass("FirstSet",
representation(
Anothernumber = "numeric"))
setGeneric(
name = "FirstSet",
def = function(object){standardGeneric("FirstSet")}
)
setMethod("initialize",
signature(.Object = "FirstSet"),
function (.Object, id = character(), multiplier = numeric())
{ x = id@Anumber
y = x * multiplier
.Object@Anothernumber = y
return(.Object)
}
)
setClass("Super", contains = c("Details", "FirstSet"))
setGeneric("Super",
def = function(object){standardGeneric("Super")})
setMethod("initialize",
signature(.Object = "Super"),
function(.Object, id = character(), Anumber = numeric()){
Details <- new("Details", ID = id, Anumber = Anumber)
FirstSet <- new("FirstSet", Anothernumber = Anothernumber)
Super <- new("Super", Details, FirstSet)
return(.Object)
})