Re: [Rd] NAMESPACE, S4, and .onLoad

From: Prof Brian Ripley <>
Date: Tue 22 Nov 2005 - 09:10:45 GMT

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,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Tue Nov 22 20:21:39 2005

This archive was generated by hypermail 2.1.8 : Tue 22 Nov 2005 - 11:21:09 GMT