[Rd] array subsetting of S4 object that inherits from "array"

Martin Maechler maechler at stat.math.ethz.ch
Fri Mar 6 08:59:48 CET 2009


>>>>> "BB" == Bradley Buchsbaum <bbuchsbaum at berkeley.edu>
>>>>>     on Thu, 5 Mar 2009 21:16:40 -0500 writes:

    BB> Hi,
    BB> I have an S4 class that inherits from "array" but does not add generic
    BB> implementations of the "[" method.

    BB> A simplified example is:

    BB> setClass("fooarray", contains="array")

    BB> If I create a "fooarray" object and subset it with a one-dimensional
    BB> index vector, the return value is of class "fooarray". Other variants
    BB> (see below), however, return primitive values consistent with
    BB> "ordinary" array subsetting.

    BB> x <- new("fooarray", array(0,c(10,10,10)))

    BB> class(x[1,1,1])        # prints "numeric"
    BB> class(x[1,,])           # prints "matrix"
    BB> class(x[1])             #  prints "fooarray"
    BB> class(x[1:10])        #  prints "fooarray"


    BB> This behavior seems to have been introduced in R2.8.1 as I have not
    BB> encountered it before. I tested it on R.2.7.0 and confirmed that
    BB> class(x[1]) returned "numeric".

    BB> In my case, the desired behavior is for array subsetting in all cases
    BB> to return primitive data structures, so if there is a way to override
    BB> the new behavior I would opt for that.

Yes,  the new behavior was introduced (into R 2.8.0) by me,
and ... coincidence ?! ...  two days ago, in e-talking with John
Chambers, I have been convinced, that the new feature really has
been a mis-feature.  Consequentley, yesterday (!) I'v committed
changes to both R-patched (2.8.1 patched) and R-devel which we
revert the mis-feature.

So, the override is to use  "2.8.1 patched" (or newer).

I'm sorry for my thinko that may also affect other
R-S4-programmers [of course I hope "not", but then there's
Murphy's law].

Regards,
Martin Maechler, ETH Zurich




    BB> Regards,

    BB> Brad Buchsbaum

    BB> R version 2.8.1 (2008-12-22)
    BB> i386-pc-mingw32

    BB> locale:
    BB> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
    BB> States.1252;LC_MONETARY=English_United
    BB> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

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



    BB> -- 
    BB> Bradley R. Buchsbaum
    BB> Rotman Research Institute
    BB> 3560 Bathurst St.
    BB> Toronto, ON Canada M6A 2E1
    BB> email: bbuchsbaum at rotman-baycrest.on.ca

    BB> ______________________________________________
    BB> R-devel at r-project.org mailing list
    BB> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list