Debian Codegraph @

R/staticimports.R

raw
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 if (is.null(version)) {
20 return(installed)
21 }
22 installed && isTRUE(get_package_version(pkg) >= version)
23 }
24
25 # A wrapper for `system.file()`, which caches the results, because
26 # `system.file()` can be slow. Note that because of caching, if
27 # `system_file_cached()` is called on a package that isn't installed, then the
28 # package is installed, and then `system_file_cached()` is called again, it will
29 # still return "".
30 system_file_cached <- local({
31 pkg_dir_cache <- character()
32
33 function(..., package = "base") {
34 if (!is.null(names(list(...)))) {
35 stop("All arguments other than `package` must be unnamed.")
36 }
37
38 not_cached <- is.na(match(package, names(pkg_dir_cache)))
39 if (not_cached) {
40 pkg_dir <- system.file(package = package)
41 pkg_dir_cache[[package]] <<- pkg_dir
42 } else {
43 pkg_dir <- pkg_dir_cache[[package]]
44 }
45
46 file.path(pkg_dir, ...)
47 }
48 })