4

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

就像是

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

问题是我无法定义 ... 的类,以便正确完成调度。任何想法?

4

1 回答 1

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"
于 2013-02-04T04:06:56.660 回答