-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.R
29 lines (29 loc) · 1.13 KB
/
helpers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
.check_req_packages <- function(x, note = "") {
res <- unlist(suppressWarnings(lapply(x, requireNamespace, quietly = TRUE)))
if (!all(res)) {
if (!interactive()) {
stop(note, "Some required packages are not installed. Make sure you have
these packages: ", paste0(x[!res], collapse = ", "),
call. = FALSE
)
}
op <- options("warn")
on.exit(options(op))
options(warn = 1)
warning(note, "The following packages are required for this function but
cannot be loaded: ", paste0(x[!res], collapse = ", "),
call. = FALSE)
choice <- readline(prompt = "Should I try to install these packages? (Y/N)")
if (choice %in% c("Y", "y")) {
utils::install.packages(x[!res])
res <- unlist(suppressWarnings(lapply(x, requireNamespace, quietly = TRUE)))
if (!all(res)) {
stop("Not all packages could be installed successfully. The following could still not be loaded: ", paste0(x[!res], collapse = ", "),
call. = FALSE
)
}
return(TRUE)
}
stop("Cannot proceed without these packages.", call. = FALSE)
}
}