用3个点定义S4方法

时间:2013-02-04 03:07:27

标签: r dispatch s4

我正在尝试为我创建的对象定义“c”方法。

类似

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

问题是我无法定义...的类,以便正确完成调度。 有什么想法吗?

1 个答案:

答案 0 :(得分:5)

在阐述@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"