S4对象组成不当行为?

nan*_*nue 3 r object s4

我有以下问题(代码如下):我有两个S4类,让我们用A和指定它们B.本B类有一个类型的对象,一个名为列表a.list.本A类有一个名为方法test().然后,我创建类型的对象A,叫a,和类型的对象B,b,然后我插入a在列表中的对象b@a.list.

当我提取a对象并在其中使用该test方法时,会发生以下错误:

Error en function (classes, fdef, mtable)  : 
  unable to find an inherited method for function "test", for signature "list"
Run Code Online (Sandbox Code Playgroud)

但我直接在a对象中使用该方法,一切正常.

知道我做错了什么吗?

提前致谢

现在,代码:

> setClass("A", representation(a="character", b="numeric"))
> a <- new("A", a="Adolfo", b = 10)
> a
An object of class "A"
Slot "a":
[1] "Adolfo"

Slot "b":
[1] 10

> print(a)
An object of class "A"
Slot "a":
[1] "Adolfo"

Slot "b":
[1] 10

> setClass("B", representation(c="character", d="numeric", a.list="list"))
> b <- new("B", c="chido", d=30, a.list=list())
> b
An object of class "B"
Slot "c":
[1] "chido"

Slot "d":
[1] 30

Slot "a.list":
list()

> b@a.list["objeto a"] <- a
> b
An object of class "B"
Slot "c":
[1] "chido"

Slot "d":
[1] 30

Slot "a.list":
$`objeto a`
An object of class "A"
Slot "a":
[1] "Adolfo"

Slot "b":
[1] 10

> setGeneric(name="test", 
+            def = function(object,...) {standardGeneric("test")}
+            )
[1] "test"

> setMethod("test", "A",
+           definition=function(object,...) {
+ cat("Doing something to an A object....\n")
+ }
+ )
[1] "test"
> b@a.list[1]
$`objeto a`
An object of class "A"
Slot "a":
[1] "Adolfo"

Slot "b":
[1] 10

> test(b@a.list[1])
Error en function (classes, fdef, mtable)  : 
  unable to find an inherited method for function "test", for signature "list"
> test(a)
Doing something to a....
> 
Run Code Online (Sandbox Code Playgroud)

再次感谢...

Sac*_*amp 6

您必须使用双方括号提取列表的单个元素:

 test(b@a.list[[1]])
Run Code Online (Sandbox Code Playgroud)

如果使用单个方括号,则索引列表的子集,该列表仍然只是一个列表,而不是类A:

> class(b@a.list[1])
[1] "list"

> class(b@a.list[[1]])
[1] "A"
attr(,"package")
[1] ".GlobalEnv"
Run Code Online (Sandbox Code Playgroud)