| 1 |
# Generated by staticimports; do not edit by hand. |
| 2 |
# ====================================================================== |
| 3 |
# Imported from pkg:staticimports |
| 4 |
# ====================================================================== |
| 5 |
|
| 6 |
get_package_version <- function(pkg) { |
| 7 |
# `utils::packageVersion()` can be slow, so first try the fast path of |
| 8 |
# checking if the package is already loaded. |
| 9 |
ns <- .getNamespace(pkg) |
| 10 |
if (is.null(ns)) { |
| 11 |
utils::packageVersion(pkg) |
| 12 |
} else { |
| 13 |
as.package_version(ns$.__NAMESPACE__.$spec[["version"]]) |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
is_installed <- function(pkg, version = NULL) { |
| 18 |
installed <- isNamespaceLoaded(pkg) || nzchar(system_file_cached(package = pkg)) |
| 19 |
|
| 20 |
if (is.null(version)) { |
| 21 |
return(installed) |
| 22 |
} |
| 23 |
|
| 24 |
if (!is.character(version) && !inherits(version, "numeric_version")) { |
| 25 |
# Avoid https://bugs.r-project.org/show_bug.cgi?id=18548 |
| 26 |
alert <- if (identical(Sys.getenv("TESTTHAT"), "true")) stop else warning |
| 27 |
alert("`version` must be a character string or a `package_version` or `numeric_version` object.") |
| 28 |
|
| 29 |
version <- numeric_version(sprintf("%0.9g", version)) |
| 30 |
} |
| 31 |
|
| 32 |
installed && isTRUE(get_package_version(pkg) >= version) |
| 33 |
} |
| 34 |
|
| 35 |
# A wrapper for `system.file()`, which caches the package path because |
| 36 |
# `system.file()` can be slow. If a package is not installed, the result won't |
| 37 |
# be cached. |
| 38 |
system_file_cached <- local({ |
| 39 |
pkg_dir_cache <- character() |
| 40 |
|
| 41 |
function(..., package = "base") { |
| 42 |
if (!is.null(names(list(...)))) { |
| 43 |
stop("All arguments other than `package` must be unnamed.") |
| 44 |
} |
| 45 |
|
| 46 |
not_cached <- is.na(match(package, names(pkg_dir_cache))) |
| 47 |
if (not_cached) { |
| 48 |
pkg_dir <- system.file(package = package) |
| 49 |
if (nzchar(pkg_dir)) { |
| 50 |
pkg_dir_cache[[package]] <<- pkg_dir |
| 51 |
} |
| 52 |
} else { |
| 53 |
pkg_dir <- pkg_dir_cache[[package]] |
| 54 |
} |
| 55 |
|
| 56 |
file.path(pkg_dir, ...) |
| 57 |
} |
| 58 |
}) |