Skip to contents

Tests whether package_name is installed via requireNamespace and memoizes the result in a package-level environment (package_cache), so that repeated checks for the same package within an R session pay the namespace-lookup cost only once. EDI uses this to guard optional code paths that depend on Suggests-only packages (e.g. quantreg, betareg, nbpMatching, geepack, icenReg) that are not installed automatically with the package, issuing an informative stop()/warning() and falling back to an internal implementation when the dependency is absent, rather than failing with an opaque "could not find function" error.

Usage

check_package_installed(package_name)

Arguments

package_name

Character scalar. The name of the package to check (as passed to requireNamespace()).

Value

Logical scalar. TRUE if the package is installed and its namespace can be loaded, FALSE otherwise.

Details

Caching / mutation semantics. This function has a side effect: on the first call for a given package_name in the current R session, it assigns the boolean result of requireNamespace() into the package-global environment package_cache, keyed by package_name. All subsequent calls for the same package_name (from anywhere in the package, or from user code) read the cached value directly and do not re-query the namespace registry. This means the result reflects whether the package was installed at the time of the first call; installing or removing package_name later in the same R session will not be picked up. The cache is a plain environment (not an R6 object) shared by all callers within the process and is not reset between calls; it is, however, re-initialized fresh in each new R session.

Determinism. For a fixed installed-package state, check_package_installed() is deterministic and side-effect-free beyond the memoization described above; it does not consume random number generator state and involves no numerical computation.

Lifecycle. Internal utility (exported for reuse within the package's own R6 classes across files, not intended as a general-purpose user-facing API); prefer requireNamespace() directly for one-off checks in user code.

See also

requireNamespace, on which this function is a thin memoizing wrapper.