From j.skoien at geo.uu.nl  Tue Jun 30 15:27:23 2009
From: j.skoien at geo.uu.nl (Jon Olav Skoien)
Date: Tue, 30 Jun 2009 15:27:23 +0200
Subject: [Rd] Conditional dependency between packages
Message-ID: <4A4A12BB.6020504@geo.uu.nl>

Hi,

I have already asked a similar question twice without response on the 
r-help list https://stat.ethz.ch/pipermail/r-help/2009-June/200300.html
but this list might be more appropriate. If there is a particular reason 
for the lacking answers (unclear, missing information, the solution is 
obvious to everyone except me, etc), I would like to know. The 
description below is generalized, but I can send the source code 
off-list if anyone would like to test.

I work on two packages, pkg1 and pkg2 (in two different projects). pkg1 
is quite generic, pkg2 tries to solve a particular problem within same 
field (geostatistics). Therefore, there might be users who want to use 
pkg2 as an add-on package to increase the functionality of pkg1. In 
other words, functions in pkg1 are based on the S3 class system, and I 
want pkg2 to offer methods for pkg2-objects to functions defined in 
pkg1, for users having both packages installed. Merging the packages or 
making pkg2 always depend pkg1 would be the easiest solution, but it is 
not preferred as most users will only be interested in one of the packages.

I thought this could be solved by including the following in NAMESPACE 
of pkg2:

if ("pkg1" %in% rownames(utils:::installed.packages()) {
importFrom(pkg1, fun1, fun2, fun3)
S3method(fun1, class2)
S3method(fun2, class2)
S3method(fun3, class2)
}

Unfortunately, this doesn't seem to work as I expected, particularly 
when I try to build a binary for Windows-users (R CMD build -binary 
pkg2   or R CMD INSTALL --build pkg2). It seems for me that the 
dependency between pkg2 and pkg1 is defined at the time of building a 
binary, not when the package is loaded with library(pkg2):

If I have pkg1 installed when I build pkg2, the package will not load 
for users who do not have pkg1 installed:
 > install.packages("d:/pkg2_0.1-1.zip", repos=NULL)
 > library(pkg2)
(...)
Error : object 'fun1' not found whilst loading namespace 'pkg2'
Error: package/namespace load failed for 'pkg2'

If I remove pkg1 from the library before building pkg2, pkg2 can be 
installed and loaded by everyone, but the new method is not visible for 
those with pkg1 installed:
 > install.packages("d:/pkg2_0.1-2.zip", repos=NULL)
 > library(pkg1)
 > library(pkg2)
 > methods(fun1)
[1] fun1.class1*
  Non-visible functions are asterisked

I have tried with and without one of the following:
Suggests: pkg1
Enhances: pkg1
in the DESCRIPTION file of pkg2, but I cannot see that either of them 
make a difference.

I can of course solve this problem temporarily, building two different 
versions of pkg2, with and without pkg1 installed on my computer. But 
first of all, that seems unnecessary complicated, and more important, 
the plan is also to upload these packages to CRAN where I doubt that 
such a two-version solution is possible or accepted.

Is there a way to build one binary that works both with and without 
having pkg1 installed? I have tried to search in mail archives and 
"Writing R extensions", but either it is not there, or I have missed it.

Thanks,
Jon

BTW, I am using R version 2.9.1 and Rtools version 29 under Windows XP-SP3



[Rd] Conditional dependency between packages
Jon Olav Skoien