R/packages.R
Require.RdThis is an "all in one" function that will run install.packages for
CRAN packages, devtools::install_github for GitHub.com packages and
will install specific versions of each package if
there is a packageVersionFile supplied.
Plus, when packages is provided as a character vector, or a
packageVersionFile is supplied, all package dependencies
will be first assessed for unique(dependencies) so the same package is
not installed multiple times. Finally library is called on the
packages. If packages are already installed (packages supplied),
and their version numbers are exact (when packageVersionFile is supplied),
then the "install" component will be skipped very quickly with a message.
Require(packages, packageVersionFile, libPath = .libPaths()[1], install_githubArgs = list(), install.packagesArgs = list(), standAlone = FALSE, repos = getOption("repos"), forget = FALSE)
| packages | Character vector of packages to install via
|
|---|---|
| packageVersionFile | If provided, then this will override all |
| libPath | The library path where all packages should be installed, and looked for to load
(i.e., call |
| install_githubArgs | List of optional named arguments, passed to install_github |
| install.packagesArgs | List of optional named arguments, passed to install.packages |
| standAlone | Logical. If |
| repos | The remote repository (e.g., a CRAN mirror), passed to either
|
| forget | Internally, this function identifies package dependencies using a memoised
function for speed on reuse. But, it may be inaccurate in some cases,
if packages were installed manually by a user. Set this to |
standAlone will either put the Required packages and their dependencies
all within the libPath (if TRUE) or if FALSE will only install
packages and their dependencies that are otherwise
not installed in .libPaths(), i.e., the personal or base library paths. Any
packages or dependencies that are not yet installed will be installed in libPath.
Importantly, a small hidden file (named ._packageVersionsAuto.txt)
will be saved in libPath that will store the information about
the packages and their dependencies, even if the version used is located in
.libPaths(), i.e., not the libPath provided.
This hidden file will be used if a user runs pkgSnapshot,
enabling a new user to rebuild the entire dependency chain, without having to
install all packages in an isolated directory (as does packrat).
This will save potentially a lot of time and disk space, and yet maintain reproducibility.
NOTE: since there is only one hidden file in a libPath, any call to
pkgSnapshot will make a snapshot of the most recent call to Require.
To build a snapshot of the desired packages and their versions, first run Require
with all packages, then pkgSnapshot. If a libPath is used, it must be used
in both functions.
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 saved
automatically (with standAlone = TRUE or standAlone = FALSE),
allowing a call to pkgSnapshot when a more permanent record of versions can be made.
This function will use memoise internally to determine the dependencies
of all packages. This will speed up subsequent calls to Require
dramatically. However, it will not take into account version numbers for this
memoised step. If package versions are updated manually by the user, then this cached
element should be wiped, using forget = TRUE.
# NOT RUN { # simple usage, like conditional install.packages then library Require("stats") # analogous to require(stats), but slower because it checks for # pkg dependencies, and installs them, if missing tempPkgFolder <- file.path(tempdir(), "Packages") # use standAlone, means it will put it in libPath, even if it already exists # in another local library (e.g., personal library) Require("crayon", libPath = tempPkgFolder, standAlone = TRUE) # make a package version snapshot packageVersionFile <- file.path(tempPkgFolder, ".packageVersion.txt") pkgSnapshot(libPath=tempPkgFolder, packageVersionFile) # confirms that correct version is installed Require("crayon", packageVersionFile = packageVersionFile) # Create mismatching versions -- desired version is older than current installed # This will try to install the older version, overwriting the newer version desiredVersion <- data.frame(instPkgs="crayon", instVers = "1.3.2", stringsAsFactors = FALSE) write.table(file = packageVersionFile, desiredVersion, row.names = FALSE) # won't work because newer crayon is loaded Require("crayon", packageVersionFile = packageVersionFile) # unload it first detach("package:crayon", unload = TRUE) # run again, this time, correct "older" version installs in place of newer one Require("crayon", packageVersionFile = packageVersionFile) # Mutual dependencies, only installs once -- e.g., httr Require(c("cranlogs", "covr"), libPath = tempPkgFolder) # }