From 121128df030e7d9fef75cb152a05eea981be2358 Mon Sep 17 00:00:00 2001 From: Michael Hahsler Date: Wed, 4 Aug 2021 15:35:52 -0500 Subject: [PATCH] reformated. --- R/check_installed.R | 93 +++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/R/check_installed.R b/R/check_installed.R index cf7795d..67bf160 100644 --- a/R/check_installed.R +++ b/R/check_installed.R @@ -1,44 +1,55 @@ -## this is a modified version from package rlang that only uses base R functionality. +## This is a modified version from package rlang that only uses base R functionality. -check_installed <- function (pkg, reason = NULL) -{ - if (!is.character(pkg)) { - stop("`pkg` must be a package name or a vector of package names.") - } - - needs_install <- - sapply(pkg, function(x) - !requireNamespace(x, - quietly = TRUE)) - - if (any(needs_install)) { - missing_pkgs <- pkg[needs_install] - missing_pkgs_enum <- paste(missing_pkgs, collapse = ", ") - n <- length(missing_pkgs) - - info <- - paste0("The ", missing_pkgs_enum, - " package(s) is/are required") - if (is.null(reason)) { - info <- paste0(info, ".") - } - else { - info <- paste(info, reason) - } - - if (!interactive()) { - stop(info) - } - - question <- - "Would you like to install the package(s)?" - cat(info, "\n", question) - if (utils::menu(c("Yes", "No")) != 1) { - invokeRestart("abort") +## action can be "install" (from CRAN), "stop" (with message), "check" (returns TRUE/FALSE) +## manual can be either TRUE or a string with installation instructions. +check_installed <- + function (pkg, + action = "install", + message = NULL) + { + action <- match.arg(action, choices = c("install", "stop", "check")) + + if (!is.character(pkg)) + stop("`pkg` must be a package name or a vector of package names.") + + needs_install <- + sapply(pkg, function(x) + ! requireNamespace(x, + quietly = TRUE)) + + if (action == "check") + return(!any(needs_install)) + + if (any(needs_install)) { + if (!interactive()) + stop(info) + + missing_pkgs <- pkg[needs_install] + missing_pkgs_enum <- paste(missing_pkgs, collapse = ", ") + + info <- + paste("The", missing_pkgs_enum, + "package(s) is/are required.") + + if (action == "install") { + question <- + "Would you like to install the package(s)?" + cat(info, "\n", question, sep = '') + if (utils::menu(c("Yes", "No")) != 1) { + invokeRestart("abort") + } + + utils::install.packages(missing_pkgs) + } else { + ### this is stop + cat(info, + "\n", + message, + sep = '') + + invokeRestart("abort") + } } - - utils::install.packages(missing_pkgs) - + + invisible(TRUE) } - invisible(TRUE) -} \ No newline at end of file