[R] Problems with conditional importFrom in NAMESPACE

Jon Olav Skoien j.skoien at geo.uu.nl
Wed Jun 3 14:03:15 CEST 2009


Hi,

I am currently involved in the development of two R-packages, pkg1 and 
pkg2. They should not be dependent on each other, as most users will 
only be interested in one of them. Still, I want  pkg2 to provide one 
extra S3 method for three functions (fun1-3) in pkg1 for objects of a 
class defined in pkg2 (class2), for those users who actually have both 
packages installed.

I think this should be possible to solve by having a conditional 
importFrom 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)
}

What causes trouble for me is that the dependency of pkg1 seems to be 
defined at the time of building a binary of pkg2 for Windows users, not 
when the package is loaded (using R CMD BUILD -binary pkg2, R-version 
2.9.0 and Rtools version 29 under Windows XP-SP3).

If I have pkg1 installed when I build pkg2, the package will not load 
for people who do not have pkg1 installed:
Error in loadNamespace(i[[1L]], c(lib.loc, .libPaths())) :
  there is no package called 'pkg1'
Error: package/namespace load failed for 'pkg2'

If I remove pkg1 from the library before building pkg2, pkg2 can be 
loaded by everyone, but the new method is not visible for those with 
pkg1 installed:
 > 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, 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. But first of all, that seems 
unnecessary, and more important, the plan is also to upload these 
packages to CRAN where I doubt that such a two-version solution is possible.

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




More information about the R-help mailing list