Skip to contents

Lightweight method to check if an R package is installed

Usage

check_pkg_installed(x, useMethod = c("packagedir", "requireNamespace"), ...)

Arguments

x

character string of package or packages to test.

useMethod

character default "packagedir" with the method of package confirmation.

  • "packagedir" provides a rapid test for the presence of an R package, without loading the package namespace. It tests whether system.file(package=x) returns a non-empty value, then 'DESCRIPTION' file exists in the package directory. It answers the question: "Is 'x' package installed?" It does not answer: "Is 'x' package usable in the current R session?" When useMethod also includes "requireNamespace", for any FALSE result it will also perform a secondary check as well, to confirm the package cannot be loaded by another mechanism.

  • "requireNamespace" uses requireNamespace(x, quietly=TRUE), with slight benefit that it accepts multiple values for x, and returns the result without using invisible(). This method loads the package namespace, but does not attach it. This method therefore takes the same time as loading the package, in return for providing the most accurate answer to the question: "Is 'x' package usable by this R session right now?"

...

additional arguments are ignored.

Value

logical vector indicating whether each value in x represents an installed R package. The vector is named by packages provided in x.

Details

There are many methods to test for an installed package. Most approaches incur some time or resource penalty, so check_pkg_installed() is motivated for rapid results without loading the package namespace.

This function also accepts multiple values for x for convenience.

There are two available methods defined by useMethod:

  1. useMethod="packagedir" confirms: this function represents possibly the most gentle and rapid approach. It simply calls system.file(package=x), for each entry of x, then checks these requirements:

    • Does the package directory exist via system.file(package=x)

    • Does the package directory contain the file 'DESCRIPTION'?

    • It does not check whether the package can be loaded into the current R session.

  2. useMethod="requireNamespace" confirms:

    • requireNamespace(x, quietly=TRUE) returns TRUE

    • It therefore loads the package namespace to confirm, but does not attach the package to the current session. It therefore may take time and resources, despite not altering the R environment search path.

The default behavior first tests by "packagedir", then for any FALSE results it also tests "requireNamespace".

Examples

check_pkg_installed("methods")
#> methods 
#>    TRUE 

check_pkg_installed(c("jamba",
   "multienrichjam",
   "venndir",
   "methods",
   "blah"))
#>          jamba multienrichjam        venndir        methods           blah 
#>           TRUE           TRUE           TRUE           TRUE          FALSE