[Rd] Re: [R] setClassUnion

John Chambers jmc at research.bell-labs.com
Thu Oct 14 23:06:45 CEST 2004


Matthias.Kohl at uni-bayreuth.de wrote:
> 
> Hello,
> 
> I have a question concerning "setClassUnion".

It's not really about setClassUnion, but a bug in handling slots in
prototypes whose class extends a locally defined class (a class union is
an example, but others would fail the same way).

> I'm working with R 2.0.0 Patched (2004-10-06) on windows 2000.
> 
> I tried to use "setClassUnion" in a package I am currently working on. The
> situation is similar to the following example:
> 
> The DESCRIPTION file has entries:
>   Depends: R (>= 2.0.0), methods
>   Imports: methods
>   LazyLoad: yes
> 
> The NAMESPACE file has entries:
>   importClassesFrom("methods", "NULL", "numeric")
>   exportClass("OptionalNumeric", "class1", "class2")
> 
> The example R code is:
> .onLoad <- function(lib, pkg){
>     require("methods", character = TRUE, quietly = TRUE)
> }
> 
> setClassUnion("OptionalNumeric", c("numeric", "NULL"))
> 
> setClass("class1",
>   representation(test1 = "OptionalNumeric"),
>   prototype(test1 = numeric(1)))
> 
> # why does this not work?
> # The error I get is:
> # Error in makePrototypeFromClassDef(properties, ClassDef, immediate,
> # where) :In making the prototype for class "class1" elements of the
> # prototype failed to match the corresponding slot class: test1
> # (class "OptionalNumeric ")
> # Sourcing this into R gives no error for me
> 
> # but instead using
>   prototype(test1 = NULL)
> # works
> 
> # Moreover, using the second version (with test1 = NULL)
> # the following works, too
> setClass("class2",
>   representation(test2 = "class1"),
>   prototype(test2 = new("class1", test1 = numeric(1))))
> 
> What am I doing wrong?

Nothing in the example, although this mail was more appropriate for
r-devel than r-help (see the posting guide), so I've rerouted the reply.

The definition of OptionalNumeric is not generally visible during
installation, and the code that is testing the prototype is not finding
the definition when testing, in effect, is(numeric(1),
"OptionalNumeric").

We fixed a number of similar problems for 2.0, but this seems to be one
that got through.

(NULL values are allowed in the prototype, at least for now, which is
why changing test1 to NULL works; the test2 example works because the
prototype has exactly the expected class, so no definition is needed).

The fix is simple, though I may not get a chance to test & commit it for
a couple of days.


> Can someone please explain this to me?
> 
> Thanks for your help,
> Matthias
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc



More information about the R-devel mailing list