Open maxheld83 opened 1 month ago
I'm running into the exact same scenario.
I couldn't for the life of me figure out how to combine try_fetch()
+ check_installed()
in a way that would produce the desired behaviour.
@teunbrand in case that's helpful I got this to work:
#' Checks if a package is installed and *informs* the user if not
#'
#' This is wrapper around [rlang::check_installed];
#' instead of erroring out if the check fails it returns `FALSE`.
#' However, unlike [rlang::is_installed], it emits a message to the user.
#'
#' @inheritParams rlang::check_installed
#' @inheritDotParams rlang::check_installed
#' @example inst/examples/dependencies/is_installed2/missing.R
#' @example inst/examples/dependencies/is_installed2/present.R
#' @keywords dependencies helper
#' @export
is_installed2 <- function(...) {
if (rlang::is_installed(...)) {
return(TRUE)
}
rlang::try_fetch(
# TODO this should only interact with the user as per .frequency
# might get annoying otherwise
# but that is blocked by deep integration in rlang
rlang::check_installed(...),
error = function(cnd) {
inform_missing_pkgs(...)
}
)
rlang::is_installed(...)
}
you can replace inform_missing_pkgs()
with your own simpler rlang::inform("blah")
.
Full source, largely copied from rlang itself is here: https://github.com/dataheld/elf/blob/main/R/dependencies.R
Though it's all a bit hacky, not ready for prime time.
Thanks for sharing this code, Max!
This is similar to what I attempted but I found one gripe with this approach.
If a user selects 'No' to a prompt, the function should return FALSE
but instead no value is returned at all.
It is hard to provide a reprex as this only occurs in interactive sessions, but essentially:
x <- is_installed2("foobar")
# User should select 'No' as answer
print(x)
#> Error: object 'x' not found
Related https://github.com/r-lib/rlang/issues/1658. In dm there is a standalone file that basically extends the is_installed()
behavior to skip in tests.
I sometimes find myself in this situation:
Typically, such an
optional-package
will be aSuggests:
.If they're missing it, I'd like to inform my users about them missing
optional-package
and (once) prompt them to install it.rlang::check_installed()
is great for this except that it errors out.So I'd like to us a "combination" (loosely speaking) of the two functions which:
TRUE
/FALSE
)If you deem this useful, read on -- and I'll be happy to write up a PR with some guidance --, otherwise feel free to close 🙂