3

次の問題があります(コードは以下にあります):2つのS4クラスがあり、Aとで指定できますB。このBクラスには、という名前のAタイプオブジェクトのリストがありますa.listAクラスには、という名前のメソッドがありますtest()。次に、Aと呼ばれるタイプのオブジェクトとa、タイプBのオブジェクトを作成し、そのオブジェクトをのリストbに挿入します。ab@a.list

aオブジェクトを抽出してtestメソッドを使用すると、次のエラーが発生します。

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

しかし、私はオブジェクトで直接メソッドを使用し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....
> 

再度、感謝します...

4

1 に答える 1

6

二重角かっこを使用して、リストの単一要素を抽出する必要があります。

 test(b@a.list[[1]])

単一の角かっこを使用する場合は、リストのサブセットにインデックスを付けます。これは、クラスではなく、単なるリストですA

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

> class(b@a.list[[1]])
[1] "A"
attr(,"package")
[1] ".GlobalEnv"
于 2012-08-15T17:12:12.470 に答える