library(docopt)
libloc <- .libPaths()[1]
doc <- paste0("Usage: install2.r [-l LIBLOC] [-h] [-x] [-s] [-d DEPS] [-n NCPUS] [-r REPOS...] [-m METHOD] [-t TYPE] [--error] [--skipmissing] [--] [PACKAGES ...]
-l --libloc LIBLOC location in which to install [default: ", libloc, "]
-d --deps DEPS install suggested dependencies as well [default: NA]
-n --ncpus NCPUS number of processes to use for parallel install, -1 selects all cores [default: getOption]
-r --repos REPOS repositor(y|ies) to use, or NULL for file [default: getOption]
-e --error throw error and halt instead of a warning [default: FALSE]
--skipmissing use with the --error option, skip the packages missing error [default: FALSE]
-s --skipinstalled skip installing already installed packages [default: FALSE]
-m --method METHOD method to be used for downloading files [default: auto]
-t --type TYPE installation type as used by `install.packages` [default: getOption]
-h --help show this help text
-x --usage show help and short example usage")
opt <- docopt(doc)
if (opt$usage) {
cat(doc, "\n\n")
cat("where PACKAGES... can be one or more CRAN package names, or local (binary or source)
package files (where extensions .tar.gz, .tgz and .zip are recognised). Optional
arguments understood by R CMD INSTALL can be passed interspersed in the PACKAGES, though
this requires use of '--'.
Examples:
install2.r -l /tmp/lib Rcpp BH # install into given library
install2.r -- --with-keep.source drat # keep the source
install2.r -- --data-compress=bzip2 stringdist # prefer bz2 compression
install2.r \".\" # install package in current directory
install2.r -n 6 ggplot2 # parallel install: (6 processes)
install2.r is part of littler which brings 'r' to the command-line.
See https://dirk.eddelbuettel.com/code/littler.html for more information.\n")
q("no")
}
if (opt$deps == "TRUE" || opt$deps == "FALSE") {
opt$deps <- as.logical(opt$deps)
} else if (opt$deps == "NA") {
opt$deps <- NA
}
if (length(opt$repos) == 1 && "NULL" %in% opt$repos) {
opt$repos <- NULL
}
if ("getOption" %in% opt$repos) {
opt$repos <- c(opt$repos[which(opt$repos != "getOption")], getOption("repos"))
}
if (opt$ncpus == "getOption") {
opt$ncpus <- getOption("Ncpus", 1L)
} else if (opt$ncpus == "-1") {
opt$ncpus <- max(1L, parallel::detectCores())
}
if (opt$type == "getOption") {
opt$type <- getOption("pkgType")
}
Sys.setenv("_R_SHLIB_STRIP_"="true")
install_packages2 <- function(pkgs, ..., error = FALSE, skipmissing = FALSE, skipinstalled = FALSE) {
e <- NULL
capture <- function(e) {
if (error) {
catch <-
grepl("(download|installation) of package .* failed", e$message) ||
(grepl("(dependenc|package).*(is|are) not available", e$message) && !skipmissing) ||
grepl("installation of package.*had non-zero exit status", e$message) ||
grepl("installation of .+ package(|s) failed", e$message)
if (catch) {
e <<- e
}
}
}
if (skipinstalled) {
pkgs <- setdiff(pkgs, installed.packages()[,1])
}
if (length(pkgs) > 0) {
withCallingHandlers(install.packages(pkgs, ...), warning = capture)
if (!is.null(e)) {
stop(e$message, call. = FALSE)
}
}
}
isMatchingFile <- function(f) (file.exists(f) && grepl("(\\.tar\\.gz|\\.tgz|\\.zip)$", f)) || (f == ".")
installArg <- function(f, lib, rep, dep, iopts, error, skipmissing, skipinstalled, ncpus, method, type) {
install_packages2(pkgs=f,
lib=lib,
repos=if (isMatchingFile(f)) NULL else rep,
dependencies=dep,
INSTALL_opts=iopts,
Ncpus = ncpus,
method = method,
type = type,
error = error,
skipmissing = skipmissing,
skipinstalled = skipinstalled)
}
isArg <- grepl('^--',opt$PACKAGES)
installOpts <- opt$PACKAGES[isArg]
opt$PACKAGES <- opt$PACKAGES[!isArg]
if (length(opt$PACKAGES)==0 && file.exists("DESCRIPTION") && file.exists("NAMESPACE")) {
message("* installing *source* package found in current working directory ...")
opt$PACKAGES <- "."
}
isMatchingFile <-
function(f) (file.exists(f) &&
grepl("(\\.tar\\.gz|\\.tgz|\\.zip)$", f)) || (f == ".")
isLocal <- sapply(opt$PACKAGES, isMatchingFile)
if (any(isLocal)) {
sapply(opt$PACKAGES, installArg, opt$libloc, opt$repos, opt$deps,
installOpts, opt$error, opt$skipmissing, opt$skipinstalled, opt$ncpus, opt$method, opt$type)
} else {
install_packages2(pkgs = opt$PACKAGES,
lib = opt$libloc,
repos = opt$repos,
dependencies = opt$deps,
INSTALL_opts = installOpts,
Ncpus = opt$ncpus,
method = opt$method,
type = opt$type,
error = opt$error,
skipmissing = opt$skipmissing,
skipinstalled = opt$skipinstalled)
}
sapply(list.files(path=tempdir(), pattern="^(repos|libloc).*\\.rds$", full.names=TRUE), unlink)