This is from a private question which I'm given permission to
answer in public:
>>>>> "IF" == Ingo Feinerer <h0125130 at
wu-wien.ac.at>
>>>>> on Fri, 13 Jul 2007 16:14:07 +0200 writes:
IF> Hello, We tried to derive a class from Matrix but had
IF> some problems. Maybe you can help us:
library("Matrix")
m <- Matrix(c(1:3,rep(0,12),16:20), nrow = 4, ncol = 5)
setClass("TermDocMatrix", representation(Weighting =
"character"),
contains = ("Matrix"))
IF> Now we want to do something like:
IF> new("TermDocMatrix", .Data = m, weighting =
"foobar")
IF> which obviously does not work due to the missing .Data
IF> slot.
yes, obviously, indeed. There is never any .Data slot in our
matrices.
IF> Note that we do not know in advance what the
IF> matrix "m" actually is (we only know it is *some*
IF> Matrix, e.g., we do not know if it is a dgCMatrix or a
IF> lgCMatrix or ...). Is there a (simple) solution?
Well, yes, but probably not the one you had wanted:
setClass("TD_Matrix",
representation(data = "Matrix", Weighting =
"character"))
A <- spMatrix(10,20, i = c(1,3:8),
j = c(2,9,6:10),
x = 7 * (1:7))
tdr <- new("TD_Matrix", data = A, Weighting = "foobar")
tdr
----------------
Now I understand that and why you had wanted to do this the
original way you did - which cannot work AFAICS.
OTOH, I wonder if other useRs, particularly those who know about
S4 classes (and the "Matrix" classes), have better proposals
maybe along the following "dream" ..
I think what Ingo would want is to say:
let me extend the full Matrix class hierarchy (or just the
"dsparseMatrix" sub hierarchy) by a new slot 'Weighting'
and hence by default inherit all methods which I don't
explicitly set myself.
I think this can only partially work, even for a future version
of R, since the inherited methods don't know what to do with
"Weighting", but it would already be interesting if all
necessary new methods could be defined semi-automatically:
1) call the corresponding Matrix method
2) pass on all my extra slots
Regards,
Martin