Require-package {Require} | R Documentation |
Require: Installing and Loading R Packages for Reproducible Workflows
Description
A single key function, 'Require' that makes rerun-tolerant versions of 'install.packages' and 'require' for CRAN packages, packages no longer on CRAN (i.e., archived), specific versions of packages, and GitHub packages. This approach is developed to create reproducible workflows that are flexible and fast enough to use while in development stages, while able to build snapshots once a stable package collection is found. As with other functions in a reproducible workflow, this package emphasizes functions that return the same result whether it is the first or subsequent times running the function, with subsequent times being sufficiently fast that they can be run every time without undue waiting burden on the user or developer.
This is an "all in one" function that will run install.packages
for CRAN
and GitHub https://github.com/ packages and will install specific versions
of each package if versions are specified either via an (in)equality (e.g.,
"glue (>=1.6.2)"
or "glue (==1.6.2)"
for an exact version) or with a
packageVersionFile
. If require = TRUE
, the default, the function will
then run require
on all named packages that satisfy their version
requirements. If packages are already installed (packages
supplied), and
their optional version numbers are satisfied, then the "install" component
will be skipped.
Usage
Require(
packages,
packageVersionFile,
libPaths,
install_githubArgs = list(),
install.packagesArgs = list(INSTALL_opts = "--no-multiarch"),
standAlone = getOption("Require.standAlone", FALSE),
install = getOption("Require.install", TRUE),
require = getOption("Require.require", TRUE),
repos = getOption("repos"),
purge = getOption("Require.purge", FALSE),
verbose = getOption("Require.verbose", FALSE),
type = getOption("pkgType"),
upgrade = FALSE,
returnDetails = FALSE,
...
)
Install(
packages,
packageVersionFile,
libPaths,
install_githubArgs = list(),
install.packagesArgs = list(INSTALL_opts = "--no-multiarch"),
standAlone = getOption("Require.standAlone", FALSE),
install = TRUE,
repos = getOption("repos"),
purge = getOption("Require.purge", FALSE),
verbose = getOption("Require.verbose", FALSE),
type = getOption("pkgType"),
upgrade = FALSE,
...
)
Arguments
packages |
Either a character vector of packages to install via
|
packageVersionFile |
Character string of a file name or logical. If
|
libPaths |
The library path (or libraries) where all packages should be
installed, and looked for to load (i.e., call |
install_githubArgs |
Deprecated. Values passed here are merged with
|
install.packagesArgs |
List of optional named arguments, passed to
|
standAlone |
Logical. If |
install |
Logical or "force". If |
require |
Logical or character string. If |
repos |
The remote repository (e.g., a CRAN mirror), passed to either
|
purge |
Logical. Should all caches be purged? Default is
Internally, there are calls to |
verbose |
Numeric or logical indicating how verbose should the function
be. If -1 or -2, then as little verbosity as possible. If 0 or FALSE,
then minimal outputs; if |
type |
See |
upgrade |
When |
returnDetails |
Logical. If |
... |
Passed to |
Details
Install
is the same as Require(..., require = FALSE)
, for convenience.
Value
Require
is intended to replace base::require
, thus it returns a
logical, named vector indicating whether the named packages have been loaded.
Because Require
also has the ability to install packages, a return value of
FALSE
does not mean that it did not install correctly; rather, it means it
did not attach with require
, which could be because it did not install
correctly, or also because e.g., require = FALSE
.
standAlone
will either put the Require
d packages and their dependencies
all within the libPaths
(if TRUE
) or if FALSE
will only install
packages and their dependencies that are otherwise not installed in
.libPaths()[1]
, i.e., the current active R package directory. Any packages
or dependencies that are not yet installed will be installed in libPaths
.
GitHub Package
Follows remotes::install_github
standard. As with
remotes::install_github
, it is not possible to specify a past version of
a GitHub package unless that version is a tag or the user passes the SHA
that had that package version. Similarly, if a developer does a local
install e.g., via pkgload::install
, of an active project, this package
will not be able know of the GitHub state, and thus pkgSnapshot
will not
be able to recover this state as there is no SHA associated with a local
installation. Use Require
(or remotes::install_github
) to create a
record of the GitHub state.
Package Snapshots
To build a snapshot of the desired packages and
their versions, first run Require
with all packages, then pkgSnapshot
.
If a libPaths
is used, it must be used in both functions.
Mutual Dependencies
This function works best if all required
packages are called within one Require
call, as all dependencies can be
identified together, and all package versions will be addressed (if there
are no conflicts), allowing a call to pkgSnapshot()
to take a snapshot or
"record" of the current collection of packages and versions.
Local Cache of Packages
When installing new packages, Require
will put all source and binary files in an R-version specific subfolder of
getOption("Require.cachePkgDir")
whose default is RPackageCache()
,
meaning cache packages locally in a project-independent location, and
will reuse them if needed. To turn off this feature, set
options("Require.cachePkgDir" = FALSE)
.
Note
For advanced use and diagnosis, the user can set verbose = TRUE
or
1
or 2
(or via options("Require.verbose")
). This will attach an
attribute attr(obj, "Require")
to the output of this function.
Author(s)
Maintainer: Eliot J B McIntire eliot.mcintire@canada.ca (ORCID)
Other contributors:
Alex M Chubaty achubaty@for-cast.ca (ORCID) [contributor]
Her Majesty the Queen in Right of Canada, as represented by the Minister of Natural Resources Canada [copyright holder]
See Also
Useful links:
Report bugs at https://github.com/PredictiveEcology/Require/issues
Examples
## Not run:
opts <- Require:::.setupExample()
library(Require)
getCRANrepos(ind = 1)
Require("utils") # analogous to require(stats), but it checks for
# pkg dependencies, and installs them, if missing
# unquoted version
Require(c(tools, utils))
if (Require:::.runLongExamples()) {
# Install in a new local library (libPaths)
tempPkgFolder <- file.path(tempdir(), "Require/Packages")
# use standAlone, means it will put it in libPaths, even if it already exists
# in another local library (e.g., personal library)
Install("crayon", libPaths = tempPkgFolder, standAlone = TRUE)
# Mutual dependencies, only installs once -- e.g., cli
tempPkgFolder <- file.path(tempdir(), "Require/Packages")
Install(c("cli", "R6"), libPaths = tempPkgFolder, standAlone = TRUE)
# Mutual dependencies, only installs once -- e.g., rlang
tempPkgFolder <- file.path(tempdir(), "Require/Packages")
Install(c("rlang", "ellipsis"), libPaths = tempPkgFolder, standAlone = TRUE)
#####################################################################################
# Isolated projects -- Use a project folder and pass to libPaths or set .libPaths() #
#####################################################################################
# GitHub packages
if (requireNamespace("gitcreds", quietly = TRUE)) {
#if (is(try(gitcreds::gitcreds_get(), silent = TRUE), "gitcreds")) {
ProjectPackageFolder <- file.path(tempdir(), "Require/ProjectA")
if (requireNamespace("curl")) {
Require("PredictiveEcology/fpCompare@development",
libPaths = ProjectPackageFolder,
)
}
# No install because it is there already
Install("PredictiveEcology/fpCompare@development",
libPaths = ProjectPackageFolder,
) # the latest version on GitHub
############################################################################
# Mixing and matching GitHub, CRAN, with and without version numbering
############################################################################
pkgs <- c(
"remotes (<=2.4.1)", # old version
"digest (>= 0.6.28)", # recent version
"PredictiveEcology/fpCompare@a0260b8476b06628bba0ae73af3430cce9620ca0" # exact version
)
Require::Require(pkgs, libPaths = ProjectPackageFolder)
#}
}
Require:::.cleanup(opts)
}
## End(Not run)