[R] setClassUnion

Matthias.Kohl@uni-bayreuth.de Matthias.Kohl at uni-bayreuth.de
Thu Oct 14 15:19:49 CEST 2004


I have a question concerning "setClassUnion".
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"))

  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
  representation(test2 = "class1"),
  prototype(test2 = new("class1", test1 = numeric(1))))

What am I doing wrong?
Can someone please explain this to me?

Thanks for your help,

More information about the R-help mailing list