2013-02-04 38 views
3

我想爲我創建的對象定義「c」方法。用3個點定義S4方法

setMethod("c", 
      signature(...), 
      definition=function (...) { 
      myObject = list(...)[[1]] 
      [email protected]=lapply(list(...), FUN = function(x) slot(x, "mySlot")) 
      return(myObject) 
     } 
) 

的問題是,我不能定義類的......,這樣的調度做得好。 有什麼想法?

+0

看看'getGeneric( 「C」)' - 你定義在'x',不'...'方法。 – hadley

回答

4

詳細說明@hadley的註釋,簽名應該是你的班級的,定義應該遵循getGeneric。因此

> getGeneric("c") 
standardGeneric for "c" defined from package "base" 

function (x, ..., recursive = FALSE) 
standardGeneric("c", .Primitive("c")) 
<environment: 0x4956ab8> 
Methods may be defined for arguments: x, recursive 
Use showMethods("c") for currently available ones. 

所以

setClass("A", representation(x="numeric")) 
setMethod("c", "A", function(x, ..., recursive=FALSE) { 
    "here I am" 
}) 

> c(new("A"), new("A")) 
[1] "here I am"