Skip to content

Instantly share code, notes, and snippets.

@wrathematics
Created October 22, 2014 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wrathematics/15186518e89d115198f0 to your computer and use it in GitHub Desktop.
Save wrathematics/15186518e89d115198f0 to your computer and use it in GitHub Desktop.
Wrapper for install.packages() to install an R package only if necessary (checks library and, if installed, current package version)
install_if_needed <- function(pkgs, lib=NULL, repos=getOption("repos"),
contriburl=contrib.url(repos, type), method, available=NULL, destdir=NULL,
dependencies=NA, type=getOption("pkgType"), configure.args=getOption("configure.args"),
configure.vars=getOption("configure.vars"), clean=FALSE, Ncpus=getOption("Ncpus", 1L),
verbose = getOption("verbose"), libs_only=FALSE, INSTALL_opts, quiet=FALSE,
keep_outputs=FALSE,
check_version=TRUE,
...)
{
installed <- installed.packages()
installed <- data.frame(Package=installed[, "Package"], Version=installed[, "Version"], stringsAsFactors=FALSE)
avail <- available.packages()
avail <- data.frame(Package=avail[, "Package"], Version=avail[, "Version"], stringsAsFactors=FALSE)
for (pkg in pkgs)
{
if (any(installed$Package == pkg))
{
if (check_version)
{
installed.version <- installed[which(installed$Package == pkg), 2]
avail.version <- avail[which(avail$Package == pkg), 2]
if (installed.version != avail.version)
should.install <- TRUE
else
should.install <- FALSE
}
}
else
should.install <- TRUE
if (should.install)
{
install.packages(pkgs=pkg, lib=lib, repos=repos, contriburl=contriburl, method=method,
available=available, destdir=destdir, dependencies=dependencies, type=type,
configure.args=configure.args, configure.vars=configure.vars, clean=clean,
Ncpus=Ncpus, verbose=verbose, libs_only=libs_only, INSTALL_opts=INSTALL_opts,
quiet=quiet, keep_outputs=keep_outputs, ...)
}
else
warning(paste("Skipping", pkg))
}
invisible()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment