[R] How call method of root "class" directly?
Henrik Bengtsson
hb at maths.lth.se
Wed Nov 13 06:44:06 CET 2002
Is there a way of calling the method of the root class directly? Here is
an example explaing my question. Everything relates to S3/UseMethod
classes and methods. I have an object x of class MicroarrayMatrix, which
inherits from class Matrix. The "core" object of x is a simple matrix.
In other words, I basically have
x <- matrix(logratios, ncol=nbrOfSpots, nrow=nbrOfSlides)
class(x) <- c("MicroarrayMatrix", "Matrix")
Both these classes implement their own version of the "["() method. The
"["() method of MicroarrayMatrix will return (spot, slide) values if
called with two indices. Example:
x[spots, slides]
This is exactly the same as calling unclass(x)[spots, slides]. If one
uses three indices the (gene, replicate, slide) values according to a
spot -> (gene, replicate) map will be returned. Example:
x[gene, replicate, slides]
This overloading of "["() is really useful in some situations, but
painfully slow in other situations. For instance, trying to do (dummy
example but it explains my problem):
sumOfMeans <- 0
for (row in seq(length=nrow(x)))
sumOfMeans <- sumOfMeans + mean(x[row,])
Since x is of class MicroarrayMatrix, there will be some overhead costs
because first "["() will be called, which calls "[.MicroarrayMatrix"(),
which then calls "[.Matrix"(), which finally calls the built in "["()
function. Is there a way of calling this built in function directly? It
would speed up the calculations quite a bit. The only way I know of is
to first unclass() the object. Example:
xCopy <- unclass(x)
sumOfMeans <- 0
for (row in seq(length=nrow(x)))
sumOfMeans <- sumOfMeans + mean(xCopy[row,])
However, doing unclass() on a huge matrix also takes a while and that
approach also requires twice the memory because xCopy is a copy of x
(which I guess is the reason why unclass() takes time). I have been
trying to come up with a way of fooling NextMethod() to call the built
in function, but I can't see how to do this (without removing the "["()
methods). Also, remember that "["() is defined as .Primitive("["), so
there is no way to reach the "default" function.
Related to this topic is of course how I chose to represent the Matrix
object in the first place. I could do
x <- list(core=matrix(logratios, ncol=nbrOfSpots, nrow=nbrOfSlides))
class(x) <- c("MicroarrayMatrix", "Matrix")
instead and be able access the "pure" matrix by x$core. However, there
are pros and cons with this design too.
I appreciate any help or suggestions.
Best wishes
Henrik Bengtsson
Home: 201/445 Royal Parade, 3052 Parkville
Office: Bioinformatics, WEHI, Parkville
+61 (0)412 269 734 (cell), +61 (0)3 9387 4791 (home),
+61 (0)3 9345 2324 (lab), +1 (508) 464 6644 (global fax)
hb at wehi.edu.au, http://www.maths.lth.se/~hb/
Time zone: +11h UTC (Sweden +1h UTC, Calif. -8h UTC)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list