[Rd] Reference class inheritance using matrix/array class

EJ Nikelski nikelski at bic.mni.mcgill.ca
Thu Jun 14 22:22:19 CEST 2012


Hi,

  I'm investigating converting some S4 classes to reference classes
and have a question about inheritance. The primary data structure for
the original S4 class is a 3-d array. Using the "contains" parameter
when defining the class  permits me to inherit all of the
array-related methods. Of particular utility for me is the ability to
access the array via [] indexing. It all works quite well.

 I'm interested in converting to a Ref class since R's copy-on-change
semantics may be causing a fair bit of overhead whenever an element
within the array is changed (and the arrays can be quite large). I
also would like the inherited indexing methods to continue to work.
Looking at ?setRefClass, I note under the contains parameter:

" ... If a superclass is also a reference class, the fields and
class-based methods will be inherited."

This statement *suggests* that inheritance of methods only works for
other Ref classes, but is not explicit WRT other base classes (e.g.,
matrix, array).  I've tried a few simple dummy examples and have not
had any success.

For example, if I try:

>Vol <- setRefClass("Vol", fields=list(x="numeric"), contains="matrix")

... no error is produced, *but* on displaying Vol, I see no mention of
the "matrix" class. Carrying on, if I then try to instantiate an
object:

>Vol$methods( initialize = function() {
>   setDataPart(.self,matrix(1:100,nrow=10))
>})
vol0 <- Vol$new(22)
Error in tryCatchOne(expr, names, parentenv, handlers[[1L]]) :
 node stack overflow

 I've tried a number of permutations with no luck. Any ideas? Is this
possible with Ref classes?

-jim


> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
 [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] codetools_0.2-8 tools_2.14.1


-- 
=================================
Jim Nikelski, Ph.D.
Postdoctoral Research Fellow
Bloomfield Centre for Research in Aging
Lady Davis Institute for Medical Research
Sir Mortimer B. Davis - Jewish General Hospital
McGill University



More information about the R-devel mailing list