[Rd] NAMESPACEs and S4 classes

Martin Morgan mtmorgan at fhcrc.org
Sun Feb 17 18:21:17 CET 2008


I'd like to have two packages with S4 classes with the same name but
different implementation. To that end I create a package tmpA with

setClass("A",
         representation=representation(
           x="numeric"),
         sealed=TRUE)
setClass("B",
         representation=representation(
           x="numeric"))
B <- function(...) new("B", ...)

and a NAMESPACE having only

import(methods)
export(B)

I duplicate this package source directory structure, renaming the
duplicate package tmpB in its Description file. After R CMD
INSTALL'ing both, I

> library(tmpA)
> library(tmpB)
Error in setClass("A", representation = representation(x = "numeric"),  : 
  "A" has a sealed class definition and cannot be redefined
Error : unable to load R code in package 'tmpB'
Error: package/namespace load failed for 'tmpB'
> setClass("A", prototype(y="numeric"))
Error in setClass("A", c(y = "numeric")) : 
  "A" has a sealed class definition and cannot be redefined

It appears that, although 'where' in setClass influences the location
of the class definition, there is a global class table that means only
one class of a particular name can ever be defined. Is that the
intended behavior? 

If I create a class B in the global environment

> setClass("B", representation(y="numeric"))
[1] "B"

and then use the constructor from tmpA, I end up with an instance of
the globally defined class B, rather than the one defined in the
constructor's name space:

> B()
An object of class "B"
Slot "y":
numeric(0)

How would I write B to return an instance of B as defined in tmpA?

Thanks,

Martin

> sessionInfo()
R version 2.7.0 Under development (unstable) (2008-02-09 r44397) 
x86_64-unknown-linux-gnu 

locale:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

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

other attached packages:
[1] tmpA_1.0

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-devel mailing list