Skip to content
Prev 20274 / 63424 Next

S4 method dispatch matrixOrArray (fwd)

In thinking about this a bit more I realize that even though the
approach I discussed works on the example, it coerces A to a
vector which may be undesirable in the real application.  However, one
could still circumvent the inheritance by performing the vector to
matrix conversion in the generic itself to avoid the need for
a vector method.  This is also short, avoid the ugly matrixOrArray
class and does not get one embroiled in delicate inheritance
issues:

setGeneric("foo",
         function(A, ...) {
             cat("generic", match.call()[[1]], "\n")
             if (!is(A, "array") && is(A, "vector")) A <- matrix(A, 1)
             standardGeneric("foo")
         })

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

## Test
foo(1:4)
foo(matrix(1:4, 1, 4))
foo(array(1:4, c(1, 4, 1)))
On 4/15/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: