【问题标题】:define S4 method with 3 dots用 3 个点定义 S4 方法
【发布时间】:2013-02-04 03:07:26
【问题描述】:

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

类似

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

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

【问题讨论】:

  • 看看getGeneric("c")——你在x上定义方法,而不是...

标签: r dispatch s4


【解决方案1】:

详细说明@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"

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 2021-02-17
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2013-05-01
    • 2011-04-22
    • 1970-01-01
    相关资源
    最近更新 更多