Define S4 Method With 3 Points

I am trying to define method "c" on an object I created.

something like

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

      

The problem is that I cannot define the class ... so that the dispatch takes place as expected. Any idea?

+3


source to share


1 answer


Developing @hadley's comment, the signature should be for your class and the definition should follow getGeneric

. Hence

> 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.

      

So



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

      

and

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

      

+4


source







All Articles