[Rd] NAMESPACE, S4, and .onLoad

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 22 10:10:45 CET 2005


On Mon, 21 Nov 2005, Seth Falcon wrote:

> The Writing R Extensions manual instructs developers who use S4
> classes and methods in a package with a name space to:
>
>    There needs to be an .onLoad action to ensure that the methods package
>    is loaded and attached:
>
>         .onLoad <- function(lib, pkg) require(methods)
>
> I'm wondering if listing methods in the Depends field of the package's
> DESCRIPTION file is sufficient.  My understanding is that doing so
> will result in the methods package being loaded and attached.

It will do so when the _package_ is loaded, but not when its _namespace_ 
is loaded.  So the advice is correct, as it is always possible that other 
packages will load your package's namespace (via imports) and not 
themselves depend on the 'methods' package and be used in a session with 
a non-default set of packages including 'methods'.  (This is unlikely, of 
course, which is why this may elude detection.)

At one time (and it may still be so) loading the 'methods' namespace and 
not the 'methods' package left R in an internally inconsistent state. 
When I discussed this with John Chambers, his preference was that the 
'methods' package be loaded and attached whenever S4 methods are in use. 
The advice does reflect that (rather than, say, advising that the 
'methods' namespace be imported.)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list