Re: [Rd] S4 fails to initialize linear hierarchies with intermediate VIRTUAL classes

From: John Chambers <jmc_at_r-project.org>
Date: Thu, 18 Oct 2012 12:03:49 -0700

On 10/18/12 6:31 AM, Martin Morgan wrote:
> Initialization of this simple hierarchy
>
> A = setClass("A", representation(x="numeric"))
> setClass("B", contains=c("VIRTUAL", "A"))
> C = setClass("C", contains="B")
>
> fails (neat that setClass returns generators; I hadn't realized that
> before!)
Yes, from 2.15.0. See ?setClass and the NEWS file.

>
> > C(A())
> Error: evaluation nested too deeply: infinite recursion /
> options(expressions=)?
>
> because in the default coerce<-,C,A-method
>
> > selectMethod("coerce<-", c("C", "A"))
> Method Definition:
>
> function (from, to = "A", value)
> {
> .value <- as(from, "B", TRUE)
> as(.value, "A") <- value
> ...
>
> as(from, "B", TRUE) correctly returns an instance of C.

Hmm, correct. Direct specification of the slots is fine, but supplying a superclass object fails as you show. A "harmless" solution to this may take some thinking (suggestions welcome). Not for 2.15.2 at any rate.

A workaround is to define the inheritance relation directly. In this example:

setIs("C", "A",

        coerce = function(from) new("A", x = from_at_x),
        replace = function(from, value) {from_at_x = value_at_x; from})

Or, if it's more natural, supply an initialize() method for "C" to intercept such arguments.

>
> Also VIRTUAL isn't mentioned on ?Classes or ?setClass and isn't
> documented on class?VIRTUAL, making me wonder whether virtual classes
> are actually meant to be supported as part of S4 (they are used
> regularly in Bioconductor packages)?

Eh?? Of course they are. E.g., section 9.4 of the Software for Data Analysis reference. In particular, page 353:

"Any ordinary class definition can create a virtual class, by including the special class "VIRTUAL" in the contains= argument to setClass()."

John
>
> Martin



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 18 Oct 2012 - 19:07:20 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 19 Oct 2012 - 06:00:46 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive