Re-export all objects from a package

Situation: I want to 'extend' a package pkg1 by creating pkg2 which:

  1. adds a few additional methods that operate on the classes defined in pkg1 and
  2. re-implementing a few of the existing methods in pkg1 (e.g. changing a class's show method).

I could just make the few additions, then instruct my colleagues to use the two packages like so:

library(pkg1); library(pkg2)

Loading pkg2 after pkg1 allows the method re-definitions to take precedence while also giving the user access to the new methods in pkg2.

But, I'd much prefer to have them just use pkg2 only, so they don't have to remember which method is in which package when they use the pkg::method notation in their code.
So, to achieve this, I want to re-export all of pkg1.

I tried this in NAMESPACE, hoping it would work:

import(pkg1)
exportPattern("^[^\\.]")

... but it does not work as hoped.

Is there any automated way to specify this in the NAMESPACE file, or am I stuck explicitly having to re-export every class, method, etc. in pkg1? (Which adds maintenance, since pkg1 might have new additions periodically that I'd have to again explicitly re-export in pkg2.)

This situation is what Depends is for. Depends attaches the dependency package before loading your package, so functions from both packages are available to the user.

Generally we discourage use of Depends, because people sometimes mistakenly use it when they should use Imports, but in this case it is the correct choice.

5 Likes

Using Depends attaches pkg1 to the search path, though, which isn't exactly what I was hoping to achieve. Instead, I'd like for a user to be able to use the pkg2::obj1 notation where obj1 is defined in pkg1 then re-exported by pkg2, freeing the user to remember which object is in which package (when they choose not to use library() and instead opt for the :: notation).

I found this line buried in the official Writing R Extensions doc:

It is possible to export variables from a namespace which it has imported from other namespaces: this has to be done explicitly and not via exportPattern.

So, I guess that answers my question, and I must explicitly re-export all objects/methods/classes/etc.

1 Like

I'm also interested in this. Here is how I'm referencing and linking functions reexported from my metapackage and their backend source-code:
https://forestgeo.github.io/fgeo/articles/siteonly/reference.html

Grouping is done via custom functions pick_concept() (#' @family) and pick_package() -- which mine helpfiles retrieved with utils::hsearch_db().

Users can use metapackage::function(), and quickly find the source code stored in the backend from the Reference section of the metapackage's pkgdown website.