2012-08-15 5 views
3

다음과 같은 문제가 있습니다 (코드는 아래 참조). 나는 두 개의 S4 클래스를 가지고 있으며, 이들을 AB으로 지정할 수 있습니다. B 클래스에는 a.list이라는 A 유형 개체 목록이 있습니다. A 클래스에는 test()이라는 메서드가 있습니다. 그런 다음 유형이 Aa이라는 개체를 만들고 B, b 유형의 개체를 만든 다음 [email protected] 목록에 a 개체를 삽입합니다. 나는 거기에 a 객체 사용 다음과 같은 오류가 일어나는 test 방법을 추출 할 때S4 오브젝트 구성 오작동?

는 :

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() 

> [email protected]["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" 
> [email protected][1] 
$`objeto a` 
An object of class "A" 
Slot "a": 
[1] "Adolfo" 

Slot "b": 
[1] 10 

> test([email protected][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.... 
> 

감사합니다 ...

답변

6

당신은 이중 대괄호를 사용하여 목록의 하나의 요소를 추출해야합니다

test([email protected][[1]]) 

하나의 대괄호를 사용하는 경우 목록의 하위 집합 인 stil 목록이 아니며 수업이 아닙니다. A :

> class([email protected][1]) 
[1] "list" 

> class([email protected][[1]]) 
[1] "A" 
attr(,"package") 
[1] ".GlobalEnv" 
+0

와우! 그것은 빠르고 정확했습니다! 감사합니다. 위의 모든 +1! – nanounanue