pkg_check: Package Checks

Description Usage Arguments Details Examples

View source: R/package_checks.R

Description

Check if a package is available on the local machine and optionally verify a version.

Usage

1
pkg_check(pkgs, versions, stop = FALSE)

Arguments

pkgs

a character vector of package names to check for

versions

an optional character vector, of the same length of pkgs for the minimum version of the packages.

stop

if TRUE then an error is thrown if any of the checks fail. If FALSE (default) a logical is returned.

Details

When writing a script that will be shared it is very likely that the multiple authors/users will need to have a certain set of packages available to load. The pkg_check function will verify that the packages are available to load, this includes an optional version test, and attach the package to the search list if requested.

Testing for package versions will is done as packageVersion(x) >= version. If you need a specific version of a package you should explicitly use packageVersion(x) == version in your script.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# verify that the packages qwraps2, ggplot2, and BH are available
pkg_check(c("qwraps2", "ggplot2", "BH"))

# show that the return is FALSE if a package is not available
pkg_check(c("qwraps2", "ggplot2", "BH", "NOT a PCKG"))

# verify the version for just ggplot2
pkg_check(c("qwraps2", "ggplot2", "BH"),
          c(NA, "2.2.0", NA))

# verify the version for qwraps2 (this is expected to fail as we are looking for
# version 42.3.14 which is far too advanced for the actual package development.
pkg_check(c("qwraps2", "ggplot2", "BH"),
          c("42.3.14", "2.2.0", NA))


## Not run: 
  # You can have the function throw an error is any of the checks fail
  pkg_check(c("qwraps2", "ggplot2", "BH"),
            c("42.3.14", "2.2.0", NA),
            stop = TRUE)

## End(Not run)

## Not run: 
  # If you have missing packages that can be installed from CRAN you may find
  # the following helpful.  If this code, with the needed edits, were placed at
  # the top of a script, then if a package is missing then the current version
  # from a target repository will be installed.  Use this set up with
  # discretion, others may not want the automatic install of packages.
  pkgs <- pkg_check("<packages to install>")
  if (!pkgs) {
    install.packages(attr(pkgs, "checks")[!attr(pkgs, "checks")$available][["package"]])
  }

## End(Not run)

qwraps2 documentation built on March 7, 2021, 5:06 p.m.