[R] Checking whether specific packages (from bioconductor) are installed when loading a package

Martin Morgan mtmorgan at fredhutch.org
Wed Mar 11 14:19:20 CET 2015

On 03/11/2015 01:36 AM, Søren Højsgaard wrote:
> Dear all,
> My package 'gRbase' uses three packages from Bioconductor and these are not automatically installed when gRbase is installed. My instructions (on the package webpage) to users are therefore to run:

Treat Bioconductor packages as any other, listing them in Depends: or Imports: 
or Suggests: as described in 'Writing R Extensions'. CRAN builds packages with 
access to the Bioconductor repository. Your CRAN users chooseBioCmirror() and 
setRepositories() before using install.packages(), and Bioc dependencies are 
installed like any other dependency.

> source("http://bioconductor.org/biocLite.R"); biocLite(c("graph","RBGL","Rgraphviz"))
> When loading gRbase, it is checked whether these Bioconductor packages are available, but I would like to add a message about how to install the packages if they are not.

This functionality is provided by Depends: and Imports:, so is not relevant for 
packages listed in this part of your DESCRIPTION file. You're only asking for 
advice on packages that are in Suggests:. It does not matter that these are 
Bioconductor packages or CRAN packages or ... the packages in Suggests: are not, 
by default, installed when your package was installed (see the 'dependencies' 
argument to install.packages()).

> Does this go into .onAttach or .onLoad or elsewhere?

Or not at all. If the package belongs in Suggests: and provides some special 
functionality not needed by the package most of the time (else it would be in 
Imports: [most likely] or Depends:) then there will be some few points in the 
code where the package is used and you need to alert the user to the special 
condition they've encountered. You'll want to fully specify the package and 
function to be used


(full specification provides similar advantage to Import:'ing a symbol into your 
package, avoiding symbol look-up along the search() path, potentially getting a 
function transitive.closure() defined by the user or a package different from 
RBGL). If RBGL is not available, the above code will fail, and the user will be 
told that "there is no package called 'RBGL'".

One common strategy for nicer messages is to

     if (!requireNamespace("RBGL)")
         stop("your more tailored message")

in the few code chunks before your use of RBGL::transitive.closure(). 
requireNamespace() loads but does not attach the RBGL package, so the symbols 
are available when fully qualified RBGL:: but the package does not interfere 
with the user search() path.

Which I guess brings us to your question, and the answer is probably that if 
after the above you were to still wish to add a message at package start-up, 
then the right place would be .onLoad(), so that users of your package, as well 
as users of packages that Import: (load) but do not Depend: (attach) on your 
package, will see the message.

Also, this belongs on the R-devel mailing list.

Hope that's helpful,


> Thanks in advance
> Søren
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

More information about the R-help mailing list