我正在尝试为一个S4类编写一个子设置方法。无论我尝试什么,我都会得到this S4 class is not subsettable错误。
下面是一个很小的例子:
setClass(Class = "A", representation = representation(ID = "character"))
setClass(Class = "B", representation = representation(IDnos = "list"))
a1 <- new(Class = "A", ID = "id1")
a2 <- new(Class = "A", ID = "id2")
B1 <- new(Class = "B", IDnos = c(a1, a2))当我打字时:
B1@IDnos[[1]]我得到了我想要的:
类"A“>槽”ID“的对象:>1 "id1”
但是我想通过写这样的东西来实现:B1[1]或者如果不是B1[[1]]
在THIS的帖子中,我有了一些想法,并试图模仿作者写的东西。但这在我的案子里行不通:
setMethod("[", c("B", "integer", "missing", "ANY"),
function(x, i, j, ..., drop=TRUE)
{
x@IDnos[[i]]
# initialize(x, IDnos=x@IDnos[[i]]) # This did not work either
})
B1[1]
> Error in B1[1] : object of type 'S4' is not subsettable以下代码也不起作用:
setMethod("[[", c("B", "integer", "missing"),
function(x, i, j, ...)
{
x@IDnos[[i]]
})
B1[[1]]
> Error in B1[[1]] : this S4 class is not subsettable有什么想法吗?
发布于 2014-08-23 05:45:03
我认为你的问题是你的签名太严格了。您需要一个“整数”类。默认情况下
class(1)
# [1] "numeric"所以它实际上不是一个真正的“整数”data.type。但是当您实际指定一个整数字面值时
class(1L)
# [1] "integer"
B1[1L]
# An object of class "A"
# Slot "ID":
# [1] "id1"所以最好使用更通用的签名
setMethod("[", c("B", "numeric", "missing", "ANY"), ... )这将允许你最初的尝试
B1[2]
# An object of class "A"
# Slot "ID":
# [1] "id2"https://stackoverflow.com/questions/25458721
复制相似问题