From 76243b9b3917c109a59778306dfa10d7d841f1ef Mon Sep 17 00:00:00 2001 From: topepo Date: Wed, 1 Aug 2018 16:12:15 -0400 Subject: [PATCH] move to glue_collapse --- NAMESPACE | 4 +++- R/check_pkg_install.R | 52 ++++++++++++++++++++++++++++++++++++++++ R/utils.R | 4 ++-- man/check_pkg_install.Rd | 28 ++++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 R/check_pkg_install.R create mode 100644 man/check_pkg_install.Rd diff --git a/NAMESPACE b/NAMESPACE index d66cd96de..fc27fe3a5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand S3method(print,pkgman_install_result) +export(check_pkg_install) export(lib_activate) export(lib_create) export(lib_deactivate) @@ -24,7 +25,7 @@ importFrom(crayon,blue) importFrom(crayon,bold) importFrom(crayon,green) importFrom(glue,backtick) -importFrom(glue,collapse) +importFrom(glue,glue_collapse) importFrom(glue,glue_data) importFrom(pkgdepends,remotes) importFrom(pkginstall,install_package_plan) @@ -32,5 +33,6 @@ importFrom(prettyunits,pretty_bytes) importFrom(prettyunits,pretty_dt) importFrom(stats,setNames) importFrom(tibble,tibble) +importFrom(utils,menu) importFrom(utils,packageDescription) importFrom(utils,remove.packages) diff --git a/R/check_pkg_install.R b/R/check_pkg_install.R new file mode 100644 index 000000000..dd22af7ea --- /dev/null +++ b/R/check_pkg_install.R @@ -0,0 +1,52 @@ +#' Check for package installation and install +#' +#' Determine if the one or more packages are installed in the library path and, +#' if not, prompt the user to install them. If all packages are installed, the +#' function silently returns the package list invisibly. +#' @inheritParams pkg_install +#' @importFrom glue glue_collapse +#' @importFrom utils menu +#' @export + +check_pkg_install <- + function(pkg, + lib = .libPaths()[[1L]], + upgrade = FALSE, + num_workers = 1L, + ask = FALSE) { + installed <- rep(TRUE, length(pkg)) + for (i in seq(along = pkg)) + installed[i] <- nrow(pkg_status(pkg[i])) > 0 + + if (any(!installed)) { + pkList <- glue::glue_collapse(pkg[!installed], sep = ", ", last = " and ") + msg <- paste( + sum(!installed), + ifelse(sum(!installed) > 1, " packages are", " package is"), + " required and", + ifelse(sum(!installed) > 1, " are", " is"), + " not installed. (", + pkList, + "). Would you like to try to install", + ifelse(sum(!installed) > 1, " them", " it"), + " from CRAN now?", + sep = "" + ) + cat(msg) + if (interactive()) { + installChoice <- menu(c("yes", "no")) + if (installChoice == 1) { + pkg_install( + pkg[!installed], + lib = lib, + upgrade = upgrade, + num_workers = num_workers, + ask = ask + ) + } else + stop("Required package is missing", call. = FALSE) + } else + stop("Required package is missing", call. = FALSE) + } + invisible(pkg) + } diff --git a/R/utils.R b/R/utils.R index 0c58ca798..45ff2b8ce 100644 --- a/R/utils.R +++ b/R/utils.R @@ -42,8 +42,8 @@ is_verbose <- function() { getOption("pkg.show_progress") %||% interactive() } -#' @importFrom glue collapse backtick +#' @importFrom glue glue_collapse backtick format_items <- function (x) { - paste0(collapse(backtick(x), sep = ", ", last = " and ")) + paste0(glue_collapse(backtick(x), sep = ", ", last = " and ")) } diff --git a/man/check_pkg_install.Rd b/man/check_pkg_install.Rd new file mode 100644 index 000000000..8fd00ba16 --- /dev/null +++ b/man/check_pkg_install.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/check_pkg_install.R +\name{check_pkg_install} +\alias{check_pkg_install} +\title{Check for package installation and install} +\usage{ +check_pkg_install(pkg, lib = .libPaths()[[1L]], upgrade = FALSE, + num_workers = 1L, ask = FALSE) +} +\arguments{ +\item{pkg}{Package names or remote package specifications to install. +See \link[pkgdepends:remotes]{pkgdepends::remotes} for details about remote package +specifications.} + +\item{lib}{Package library to install the packages to.} + +\item{upgrade}{Whether to upgrade already installed packages to the +latest available version.} + +\item{num_workers}{Number of worker processes to use.} + +\item{ask}{Whether to ask for confirmation.} +} +\description{ +Determine if the one or more packages are installed in the library path and, +if not, prompt the user to install them. If all packages are installed, the +function silently returns the package list invisibly. +}