Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function to suggest installing packages #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -24,13 +25,14 @@ 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)
importFrom(prettyunits,pretty_bytes)
importFrom(prettyunits,pretty_dt)
importFrom(stats,setNames)
importFrom(tibble,tibble)
importFrom(utils,menu)
importFrom(utils,packageDescription)
importFrom(utils,remove.packages)
52 changes: 52 additions & 0 deletions R/check_pkg_install.R
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 "))
}
28 changes: 28 additions & 0 deletions man/check_pkg_install.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.