devnull
devnull

Reputation: 21

setMethod and package Matrix

I am creating an S4 class that uses package Matrix and then using setMethod to redefine "sin" for my class

> library(Matrix)
> setClass("foo",slots=list(z="Matrix"))
> setMethod("sin",signature(x="foo"),function(x){return(cos(x@z))})
[1] "sin"

however, even before I get started using my class I encounter a problem

> y<-Matrix(c(1,2,1,2),2,2)
> sin(y)
2 x 2 Matrix of class "dgeMatrix"
          [,1]      [,2]
[1,] 0.8414710 0.8414710
[2,] 0.9092974 0.9092974
> sin(y)
Error in match(x, table, nomatch = 0L) : object '.Generic' not found

Why does the second use of sin(y) fail ? This is my first attempt at programming with S4 classes. Any help will be much appreciated.

Upvotes: 1

Views: 232

Answers (2)

R大卫
R大卫

Reputation: 150

Just posted an alternative solution in a thread with a similar question https://stackoverflow.com/a/37566785/2116352 without having to overload the whole generic group. tl;dr: Overload the individual function within a package and load the package.

Upvotes: 0

Martin Morgan
Martin Morgan

Reputation: 46856

At some level this looks like a bug that should be reported to the R-devel mailing list. But sin() is a member of the Math 'group generic' (see ?GroupGenericFunctions), and one could implement

setMethod("Math", "foo", function(x) callGeneric(x@z))

Upvotes: 1

Related Questions