I'm trying to initialize and store some package specific information when a package is first loaded and have found a lot of good information on ways to do this in How to store some package specific information in R and How to run some code on load of package. However, one function I want to call during package loading verifies the inheritance relationships of several classes defined in the package, but this returns different results from what I would have expected. I can come up with different ways to work around this problem but I'm trying to understand what I'm doing wrong and was hoping someone could explain it to me.
A minimal example of the package source code to reproduce the problem follows.
# package reference classes
setClass("myclass", representation(value = "numeric"),
prototype = prototype(value = 42))
setClass("myderivedclass", contains = "myclass")
# package on load function
.onLoad <- function(lib, pkg){
if (!extends("myderivedclass", "myclass")) {
warning("something's not right")
}
}
Now during package .onLoad
, extends("myderivedclass", "myclass")
evaluates to FALSE
(triggering the warning), but of course once the package is loaded it evaluates to TRUE
, which is what I would have expected. Initializing objects with either class during .onLoad
works just fine so it's not that the class definitions aren't loaded yet.
Thank you very much for any help on how I should be doing this kind of test during .onLoad
or what I might have misunderstood about inheritance here.
Session info:
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin10.8.0 (64-bit)