[Rd] Conditional dependency between packages

Seth Falcon seth at userprimary.net
Wed Jul 1 18:23:15 CEST 2009

Hi Jon,

* On 2009-06-30 at 15:27 +0200 Jon Olav Skoien wrote:
> 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'm not sure I understand the above, I think you may have a pkg2 where
you meant pkg1, but I'm not sure it matters.

I think the short version is, pkg2 can be used on its own but will do
more if pkg1 is available.  I don't think R's packaging system
currently supports conditional dependencies as you might like.
However, I think you can get the behavior you want by following a
recipe like:

* In pkg2 DESCRIPTION, list Suggests: pkg1.

* In pkg2 code, you might define a package-level environment and 
  in .onLoad check to see if pkg1 is available.

     PKG_INFO <- new.env(parent=emptyenv())
     .onLoad <- function(libname, pkgname) {
         if (check if pkg1 is available) {
            PKG_INFO[["pkg1"]] <- TRUE

* Then your methods can check PKG_INFO[["pkg1"]].

+ seth

More information about the R-devel mailing list