Skip to content

Commit f0c5590

Browse files
committed
homebrew an install prompt
1 parent dd3b594 commit f0c5590

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

R/utilities.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,32 @@ as_unordered_factor <- function(x) {
818818
class(x) <- setdiff(class(x), "ordered")
819819
x
820820
}
821+
822+
# TODO: Replace me if rlang/#1730 gets implemented
823+
# Similar to `rlang::check_installed()` but returns boolean and misses
824+
# features such as versions, comparisons and using {pak}.
825+
prompt_install <- function(pkg, reason = NULL) {
826+
if (length(pkg) < 1 || is_installed(pkg)) {
827+
return(TRUE)
828+
}
829+
if (!interactive()) {
830+
return(FALSE)
831+
}
832+
833+
pkg <- pkg[!vapply(pkg, is_installed, logical(1))]
834+
835+
message <- "The {.pkg {pkg}} package{?s} {?is/are} required"
836+
if (is.null(reason)) {
837+
message <- paste0(message, ".")
838+
} else {
839+
message <- paste0(message, " ", reason)
840+
}
841+
question <- "Would you like to install {cli::qty(pkg)}{?it/them}?"
842+
843+
cli::cli_bullets(c("!" = message, "i" = question))
844+
if (utils::menu(c("Yes", "No")) != 1) {
845+
return(FALSE)
846+
}
847+
utils::install.packages(pkg)
848+
is_installed(pkg)
849+
}

0 commit comments

Comments
 (0)