Check if user accepted a package installation
check_installed2.RdThis function returns TRUE if pkg is already installed, and prompt the
user to install it otherwise. If the user accepts, returns TRUE, and
FALSE if he rejects. This is different from rlang::check_installed()
which will exit the function if the user rejects installation.
Useful for suggesting the user a more featureful path of the function, while still allowing a fallback path.
Any installation errors, e.g. packages not found, still bubble up.
Usage
check_installed2(
pkg,
reason = NULL,
...,
version = NULL,
compare = NULL,
action = NULL,
call = caller_env()
)Arguments
- pkg
The package names. Can include version requirements, e.g.
"pkg (>= 1.0.0)".- reason
Optional string indicating why is
pkgneeded. Appears in error messages (if non-interactive) and user prompts (if interactive).- ...
These dots must be empty.
- version
Minimum versions for
pkg. If supplied, must be the same length aspkg.NAelements stand for any versions.- compare
A character vector of comparison operators to use for
version. If supplied, must be the same length asversion. IfNULL,>=is used as default for all elements.NAelements incompareare also set to>=by default.- action
An optional function taking
pkgand...arguments. It is called bycheck_installed()when the user chooses to update outdated packages. The function is passed the missing and outdated packages as a character vector of names.- call
The execution environment of a currently running function, e.g.
caller_env(). The function will be mentioned in error messages as the source of the error. See thecallargument ofabort()for more information.
Examples
# Safely use inside functions without exiting if the package is not installed:
f <- function(x) {
if (check_installed2("crazy_print_package")) {
# If the package is installed or the user chooses to install it
# Some code such as `crazy_print_package::crazy_print(x)`
} else {
# If the package is not installed and the user refused to install it
print(x)
}
}