From ripley at stats.ox.ac.uk  Thu May 24 12:01:34 2012
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu, 24 May 2012 11:01:34 +0100
Subject: [Rd] New S3 methods for optional package
In-Reply-To: <4FBE0178.50109@jrc.ec.europa.eu>
References: <4FBE0178.50109@jrc.ec.europa.eu>
Message-ID: <4FBE06FE.7010000@stats.ox.ac.uk>

On 24/05/2012 10:38, Jon Olav Skoien wrote:
> Hi,
>
> I have asked this question before, but the solution I ended up with (see
> below) creates a note when running R CMD check. So I am trying again...
>
> I am developing a package B that, among other things, also offers some
> extra S3-methods for functions in package A if the user has installed A.
> I do not want to list A under Depends of B, as the dependency list of A
> is rather long, and most potential users of B will not be interested in
> package A and what it depends on. Unfortunately I struggle with doing
> this right. After asking on the list some time ago, I have listed A
> under Suggests, and have a .onLoad function in B with
> if (require(A)) registerS3methods(newMethodsMatrix, package = A, env =
> environment(B))

You are not supposed to be calling registerS3methods .... it is only 
visible because of the nature of the base namespace and its 
documentation says

'Not intended to be called directly.'

And require() is the wrong thing here; you want to register methods on a 
namespace.   The logic seems to be that you should do that only if A's 
namespace is already loaded, but you could load it pre-emptively with 
requireNamespace().

> But starting with R 2.13 or R 2.14, R CMD check creates a note:
> "Package startup functions should not change the search path.
> See section 'Good practice' in ?.onAttach."
> I have understood that packages with notes can be uploaded to CRAN, but
> that they tend to create extra work for the maintainers and hence I am
> trying to find another solution.

Most likely a call to requireNamespace() would work without any NOTE.

> So far I have tried:
> List A under Suggest of B, with a conditional import in NAMESPACE.
> If I build a Windows-binary from this when A is installed, this package
> can be installed but not loaded on computers where A is not installed.

That will happen.  But that's the price for the convenience of binary 
packages.

> List A under Enhances of B.
> This seems to be the right thing, as the R extensions manual says: "the
> 'Enhances' field lists packages "enhanced" by the package at hand, e.g.,
> by providing methods for classes from these packages".
> However, although it seems I can install and load package B when I
> conditionally import package A in the NAMESPACE, R CMD check stops with
> the error: Namespace dependency not required: A
> If I remove the import, R CMD check is happier, but I cannot load the
> package after installing.
>
> I have read about the use of "Suggest", "Enhances" etc in "Writing R
> Extensions", but could not figure out the right way to do this. I am
> sure there is something I am missing here.
>
> If anyone wants to check possible solutions, package A is "intamap",
> available from CRAN, whereas B is "rtop", available from Rforge:
> **|install.packages("rtop", repos="http://R-Forge.R-project.org")|**

I guess the problem is that you are trying to do too much with package 
rtop.  I would consider creating a separate package depending on intamap 
(and most likely rtop) which adds the S3 methods for intamap.

> Thanks,
> Jon

-- 
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



[Rd] New S3 methods for optional package
Prof Brian Ripley