如何创建一个将数组作为插槽的S4类?下面,我有一个示例类。我希望能够以这样一种方式构造,即我得到两个“人”元素,每个元素都有适当的数组成员。
下面的代码给出了以下错误:“validObject(.Object)中的错误: 无效的类“人”对象:类“人”中的插槽“子”的无效对象:得到类“字符”,应该是或扩展类“数组”
setClass("person", representation(name="character", age="numeric", children = "array"))
setMethod(
f = "[",
signature="person",
definition=function(x,i,j,...,drop=TRUE){
initialize(x, name=x@name[i], age = x@age[i], children = x@children[i])
}
)
setMethod(f = "length", signature = "person", definition = function(x){
length(x@name)
})
setMethod(f = "dim", signature = "person", definition = function(x){
length(x@name)
})
kids1 = as.array(c("Bob", "Joe", "Mary"))
person = new("person", name="John", age=40, children = kids1)
person@children[2]
kids2 = as.array(c("Steve", "Beth", "Kim"))
people = new("person", name=c("John", "Fred"), age=c(40, 20), children = as.array(c(kids1, kids2), dim = 2))
people[1]@age
people[2]@children[1]
答案 0 :(得分:2)
您可能不希望@children
成为数组。如果dim
属性长度为1,则它与向量基本相同,并且您无法区分不同人的孩子。考虑将此插槽改为列表。
setClass("person",
representation(name="character", age="numeric", children = "list"))
person = new("person", name="John", age=40, children = list(kids1))
person@children
people = new("person", name=c("John", "Fred"), age=c(40, 20),
children = list(kids1, kids2))
people[1]
答案 1 :(得分:1)
将drop=FALSE
添加到children
广告位的子集中;这是数组子集的标准R规则的结果
setMethod(
f = "[",
signature="person",
definition=function(x,i,j,...,drop=TRUE){
initialize(x, name=x@name[i], age = x@age[i],
children = x@children[i,drop=FALSE])
}
)
另外,我不确定你的as.array()
是按照你的想法做的? dim
参数被忽略。
答案 2 :(得分:1)
Hong的回答有效。我确实需要在“[”子集函数中添加一个列表包装器。一旦完成,一切正常。
kids1 = as.array(c("Bob", "Joe"))
kids2 = as.array(c("Steve", "Beth", "Kim"))
setClass("person", representation(name="character", age="numeric", children = "list"))
setMethod(
f = "[",
signature="person",
definition=function(x,i,j,...,drop=TRUE){
initialize(x, name=x@name[i], age = x@age[i], children = list(x@children[i]))
}
)
people = new("person", name=c("John", "Fred"), age=c(40, 20), children = list(kids1, kids2))
people[1]@name
people[1]@age
people[1]@children
people[2]@name
people[2]@age
people[2]@children