Skip to content

Commit

Permalink
reformated.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhahsler committed Aug 4, 2021
1 parent be3a8c0 commit 121128d
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions R/check_installed.R
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 121128d

Please sign in to comment.