[R] S4 method dispatch matrixOrArray

Paul Roebuck roebuck at mdanderson.org
Wed Apr 12 17:55:27 CEST 2006


I have some code where the primary dispatching is on
other parameters so I'd like not to have to create a
set of functions for "matrix" and another duplicate
set for "array". But the class union technique isn't
working as implemented below and I don't have my Green
book with me. How do I fix my infinite recursion problem?


##--------------------------------------------------------
library(methods)

setGeneric("foo",
           function(A, ...) {
               cat("generic", match.call()[[1]], "\n")
               standardGeneric("foo")
           })

setMethod("foo",
          signature(A = "vector"),
          function(A, ...) {
              callGeneric(matrix(A, nrow = 1), ...)
          })

setClassUnion("matrixOrArray", c("matrix", "array"))

setMethod("foo",
          signature(A = "matrixOrArray"),
          function(A, ...) {
              cat("A =", A, "\n")
          })

## Test
foo(1:4)
foo(matrix(1:4, 1, 4))
foo(array(1:4, c(1, 4, 1)))

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)




More information about the R-help mailing list