From renaud at mancala.cbio.uct.ac.za  Thu Jun 23 13:22:06 2011
From: renaud at mancala.cbio.uct.ac.za (Renaud Gaujoux)
Date: Thu, 23 Jun 2011 13:22:06 +0200
Subject: [Rd] Class not found when search in .onLoad
Message-ID: <4E0321DE.2020601@cbio.uct.ac.za>

Hi,

I am facing with a strange behaviour of isClass and extends when these 
are called in .onLoad in both R 2.12.1 and R 2.13.0. This is preventing 
my package from doing some object initializations at a proper place 
(i.e. in .onLoad).

Suppose one defines two S4 classes in a package, and that one needs to 
check the inheritance between these two when loading the package (e.g. 
to validate slots in objects).
See package attached or code below (not sure attachments can go through).

in R 2.13.0:
At the loading check after installation, the classes are not found by 
`isClass` and `extends` when these are called in .onLoad, but are 
correctly found when called in .onAttach.
However findClass correctly finds the class in both case, as well as 
isClass if it is called with the argument 
`where=asNamespace('<THE.PACKAGE.NAME>')`.
When the package is loaded from an open R session, the behaviour is the 
same.

in R 2.12.1:
the classes are correctly found by isClass and extends when these are 
called in .onLoad or .onAttach, but only at installation (i.e. at the 
loading check after R CMD INSTALL).
When the package is loaded from an open R session, one fails to find the 
classes only in .onLoad while in .onAttach they are correctly found.

This is really an issue as up to now I was using .onAttach to do my 
checks and initialization, but it is not a wise thing as package that 
would only need to import my package (load and not attach) will not get 
internal objects properly initialized. All this should be done in 
.onLoad, but I cannot do it due to this behaviour of `extends`.

Can someone provide some explanations or work around.

Thank you,
Renaud


############
# script.R
############

setClass('A', representation(data='numeric'))

setClass('B', contain='A')

check.classes <- function(){

a <- new('A')
b <- new('B')

message("isClass('A'): ", methods::isClass('A'))
message("isClass('A') in namespace: ", methods::isClass('A', 
where=asNamespace('anRpackage')))
message("findClass('A'): ")
print(methods::findClass('A'))

message("isClass('B'): ", methods::isClass('B'))
message("isClass('B') in namespace: ", methods::isClass('B', 
where=asNamespace('anRpackage')))
message("findClass('B'): ")
print(methods::findClass('B'))

message("extends('B', 'A'): ", methods::extends('B', 'A'))
message("is(a, 'A'): ", is(a, 'A'))
message("inherits(a, 'A'): ", inherits(a, 'A'))
message("is(b, 'A'): ", is(b, 'A'))
}

.onLoad <- function(libname, pkgname=NULL){
cat("\n## .onLoad ##\n")
check.classes()
}

.onAttach <- function(libname, pkgname){
cat("\n## .onAttach ##\n")
check.classes()
}
############



################################################
################################################
# Output
################################################
################################################

################################################
# R-2.13.0 CMD INSTALL anRpackage_1.0.tar.gz
################################################

* installing *source* package ?anRpackage? ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

## .onLoad ##
isClass('A'): FALSE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]
<environment: namespace:anRpackage>

isClass('B'): FALSE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]
<environment: namespace:anRpackage>

extends('B', 'A'): FALSE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

## .onAttach ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]
<environment: namespace:anRpackage>

isClass('B'): TRUE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]
<environment: namespace:anRpackage>

extends('B', 'A'): TRUE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

* DONE (anRpackage)


################################################
# From a R-2.12.1 Console
################################################

## .onLoad ##
isClass('A'): FALSE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]
<environment: namespace:anRpackage>

isClass('B'): FALSE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]
<environment: namespace:anRpackage>

extends('B', 'A'): FALSE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

## .onAttach ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]
<environment: namespace:anRpackage>

isClass('B'): TRUE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]
<environment: namespace:anRpackage>

extends('B', 'A'): TRUE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE


################################################
# R-2.12.1 CMD INSTALL anRpackage_1.0.tar.gz
################################################

* installing *source* package ?anRpackage? ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

## .onLoad ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]
<environment: namespace:anRpackage>

isClass('B'): TRUE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]
<environment: namespace:anRpackage>

extends('B', 'A'): TRUE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

## .onAttach ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]
<environment: namespace:anRpackage>

isClass('B'): TRUE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]
<environment: namespace:anRpackage>

extends('B', 'A'): TRUE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

* DONE (anRpackage)

################################################
# From a R-2.12.1 Console
################################################

## .onLoad ##
isClass('A'): FALSE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]
<environment: namespace:anRpackage>

isClass('B'): FALSE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]
<environment: namespace:anRpackage>

extends('B', 'A'): FALSE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

## .onAttach ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]
<environment: namespace:anRpackage>

isClass('B'): TRUE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]
<environment: namespace:anRpackage>

extends('B', 'A'): TRUE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE





 

###
UNIVERSITY OF CAPE TOWN 

This e-mail is subject to the UCT ICT policies and e-mail disclaimer published on our website at http://www.uct.ac.za/about/policies/emaildisclaimer/ or obtainable from +27 21 650 9111. This e-mail is intended only for the person(s) to whom it is addressed. If the e-mail has reached you in error, please notify the author. If you are not the intended recipient of the e-mail you may not use, disclose, copy, redirect or print the content. If this e-mail is not related to the business of UCT it is sent by the sender in the sender's individual capacity.

###
 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: anRpackage_1.0.tar.gz
Type: application/x-gzip
Size: 1722 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20110623/fd2ae106/attachment.gz>


[Rd] Class not found when search in .onLoad
Renaud Gaujoux