Search code examples
rrequiredevtoolsbioconductor

Using 'require' package code to obtain datapackages on the fly in R


I am writing an R package that uses a variety of bioconductor annotation data packages. The specific data packages vary with the use-case. As such, I have a function which does something like this:

if (!require(biocpack_name, character.only=T)) {
    source("https://bioconductor.org/biocLite.R")
    BiocInstaller::biocLite(biocpack_name)
    require(biocpack_name , character.only=T)
}

biocpack_name can be several of ~30+ annotation data packages that are looked up based on the particular data being analysed. As such, I don't want to have to add each to 'Suggests' (Im not even sure that would work because the error is not for a package but rather a string specifying the package). R CMD CHK gives me this error:

'library' or 'require' call not declared from: ‘biocpack_name’
'library' or 'require' call to ‘biocpack_name’ in package code.

How do I get around this?


Solution

  • It's not an error, but a warning. It goes away if you use character.only = TRUE rather than T (I guess because the value of TRUE is known and cannot be re-assigned, but T is unknown and can be anything, including FALSE). But in addition follow the advice in the warning to use requireNamespace() (and not pollute the user search path); maybe db = get(biocpack_name, getNamespace(biocpack_name)) will allow you to use the annotation package the way you'd like, e.g., mapIds(db, ...).

    If one were pedantic, adding the packages to the Enhances: field of the DESCRIPTION file would communicate that your package somehow works with the annotation packages, but does not result in installation of the package (e.g., for building the vignette) unless explicitly requested.