[Rd] Restrict package to load-only access - prevent attempts to attach it

Henrik Bengtsson henr|k@bengt@@on @end|ng |rom gm@||@com
Tue Jun 23 22:21:08 CEST 2020


I'm developing a package whose API is only meant to be used in other
packages via imports or pkg::foo().  There should be no need to attach
this package so that its API appears on the search() path. As a
maintainer, I want to avoid having it appear in search() conflicts by

This means that, for instance, other packages should declare this
package under 'Imports' or 'Suggests' but never under 'Depends'.  I
can document this and hope that's how it's going to be used.  But, I'd
like to make it explicit that this API should be used via imports or
::.  One approach I've considered is:

.onAttach <- function(libname, pkgname) {
   if (nzchar(Sys.getenv("R_CMD"))) return()
   stop("Package ", sQuote(pkgname), " must not be attached")

This would produce an error if the package is attached.  It's
conditioned on the environment variable 'R_CMD' set by R itself
whenever 'R CMD ...' runs.  This is done to avoid errors in 'R CMD
INSTALL' and 'R CMD check' "load tests", which formally are *attach*
tests.  The above approach passes all the tests and checks I'm aware
of and on all platforms.

Before I ping the CRAN team explicitly, does anyone know whether this
is a valid approach?  Do you know if there are alternatives for
asserting that a package is never attached.  Maybe this is more
philosophical where the package "contract" is such that all packages
should be attachable and, if not, then it's not a valid R package.

This is a non-critical topic but if it can be done it would be useful.



More information about the R-devel mailing list