| 1 |
# Generated by staticimports; do not edit by hand. |
| 2 |
# ====================================================================== |
| 3 |
# Imported from pkg:staticimports |
| 4 |
# ====================================================================== |
| 5 |
|
| 6 |
# Given a vector, return TRUE if any elements are named, FALSE otherwise. |
| 7 |
# For zero-length vectors, always return FALSE. |
| 8 |
any_named <- function(x) { |
| 9 |
if (length(x) == 0) return(FALSE) |
| 10 |
nms <- names(x) |
| 11 |
!is.null(nms) && any(nzchar(nms)) |
| 12 |
} |
| 13 |
|
| 14 |
# Given a vector, return TRUE if any elements are unnamed, FALSE otherwise. |
| 15 |
# For zero-length vectors, always return FALSE. |
| 16 |
any_unnamed <- function(x) { |
| 17 |
if (length(x) == 0) return(FALSE) |
| 18 |
nms <- names(x) |
| 19 |
is.null(nms) || !all(nzchar(nms)) |
| 20 |
} |
| 21 |
|
| 22 |
# Borrowed from pkgload:::dev_meta, with some modifications. |
| 23 |
# Returns TRUE if `pkg` was loaded with `devtools::load_all()`. |
| 24 |
devtools_loaded <- function(pkg) { |
| 25 |
ns <- .getNamespace(pkg) |
| 26 |
if (is.null(ns) || is.null(ns$.__DEVTOOLS__)) { |
| 27 |
return(FALSE) |
| 28 |
} |
| 29 |
TRUE |
| 30 |
} |
| 31 |
|
| 32 |
get_package_version <- function(pkg) { |
| 33 |
# `utils::packageVersion()` can be slow, so first try the fast path of |
| 34 |
# checking if the package is already loaded. |
| 35 |
ns <- .getNamespace(pkg) |
| 36 |
if (is.null(ns)) { |
| 37 |
utils::packageVersion(pkg) |
| 38 |
} else { |
| 39 |
as.package_version(ns$.__NAMESPACE__.$spec[["version"]]) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
is_installed <- function(pkg, version = NULL) { |
| 44 |
installed <- isNamespaceLoaded(pkg) || nzchar(system_file_cached(package = pkg)) |
| 45 |
|
| 46 |
if (is.null(version)) { |
| 47 |
return(installed) |
| 48 |
} |
| 49 |
|
| 50 |
if (!is.character(version) && !inherits(version, "numeric_version")) { |
| 51 |
# Avoid https://bugs.r-project.org/show_bug.cgi?id=18548 |
| 52 |
alert <- if (identical(Sys.getenv("TESTTHAT"), "true")) stop else warning |
| 53 |
alert("`version` must be a character string or a `package_version` or `numeric_version` object.") |
| 54 |
|
| 55 |
version <- numeric_version(sprintf("%0.9g", version)) |
| 56 |
} |
| 57 |
|
| 58 |
installed && isTRUE(get_package_version(pkg) >= version) |
| 59 |
} |
| 60 |
|
| 61 |
# Simplified version rlang:::s3_register() that just uses |
| 62 |
# warning() instead of rlang::warn() when registration fails |
| 63 |
# https://github.com/r-lib/rlang/blob/main/R/compat-s3-register.R |
| 64 |
s3_register <- function(generic, class, method = NULL) { |
| 65 |
stopifnot(is.character(generic), length(generic) == 1) |
| 66 |
stopifnot(is.character(class), length(class) == 1) |
| 67 |
|
| 68 |
pieces <- strsplit(generic, "::")[[1]] |
| 69 |
stopifnot(length(pieces) == 2) |
| 70 |
package <- pieces[[1]] |
| 71 |
generic <- pieces[[2]] |
| 72 |
|
| 73 |
caller <- parent.frame() |
| 74 |
|
| 75 |
get_method_env <- function() { |
| 76 |
top <- topenv(caller) |
| 77 |
if (isNamespace(top)) { |
| 78 |
asNamespace(environmentName(top)) |
| 79 |
} else { |
| 80 |
caller |
| 81 |
} |
| 82 |
} |
| 83 |
get_method <- function(method, env) { |
| 84 |
if (is.null(method)) { |
| 85 |
get(paste0(generic, ".", class), envir = get_method_env()) |
| 86 |
} else { |
| 87 |
method |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
register <- function(...) { |
| 92 |
envir <- asNamespace(package) |
| 93 |
|
| 94 |
# Refresh the method each time, it might have been updated by |
| 95 |
# `devtools::load_all()` |
| 96 |
method_fn <- get_method(method) |
| 97 |
stopifnot(is.function(method_fn)) |
| 98 |
|
| 99 |
# Only register if generic can be accessed |
| 100 |
if (exists(generic, envir)) { |
| 101 |
registerS3method(generic, class, method_fn, envir = envir) |
| 102 |
} else { |
| 103 |
warning( |
| 104 |
"Can't find generic `", generic, "` in package ", package, |
| 105 |
" register S3 method. Do you need to update ", package, |
| 106 |
" to the latest version?", call. = FALSE |
| 107 |
) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
# Always register hook in case package is later unloaded & reloaded |
| 112 |
setHook(packageEvent(package, "onLoad"), function(...) { |
| 113 |
register() |
| 114 |
}) |
| 115 |
|
| 116 |
# Avoid registration failures during loading (pkgload or regular). |
| 117 |
# Check that environment is locked because the registering package |
| 118 |
# might be a dependency of the package that exports the generic. In |
| 119 |
# that case, the exports (and the generic) might not be populated |
| 120 |
# yet (#1225). |
| 121 |
if (isNamespaceLoaded(package) && environmentIsLocked(asNamespace(package))) { |
| 122 |
register() |
| 123 |
} |
| 124 |
|
| 125 |
invisible() |
| 126 |
} |
| 127 |
|
| 128 |
# Borrowed from pkgload::shim_system.file, with some modifications. This behaves |
| 129 |
# like `system.file()`, except that (1) for packages loaded with |
| 130 |
# `devtools::load_all()`, it will return the path to files in the package's |
| 131 |
# inst/ directory, and (2) for other packages, the directory lookup is cached. |
| 132 |
# Also, to keep the implementation simple, it doesn't support specification of |
| 133 |
# lib.loc or mustWork. |
| 134 |
system_file <- function(..., package = "base") { |
| 135 |
if (!devtools_loaded(package)) { |
| 136 |
return(system_file_cached(..., package = package)) |
| 137 |
} |
| 138 |
|
| 139 |
if (!is.null(names(list(...)))) { |
| 140 |
stop("All arguments other than `package` must be unnamed.") |
| 141 |
} |
| 142 |
|
| 143 |
# If package was loaded with devtools (the package loaded with load_all), |
| 144 |
# also search for files under inst/, and don't cache the results (it seems |
| 145 |
# more likely that the package path will change during the development |
| 146 |
# process) |
| 147 |
pkg_path <- find.package(package) |
| 148 |
|
| 149 |
# First look in inst/ |
| 150 |
files_inst <- file.path(pkg_path, "inst", ...) |
| 151 |
present_inst <- file.exists(files_inst) |
| 152 |
|
| 153 |
# For any files that weren't present in inst/, look in the base path |
| 154 |
files_top <- file.path(pkg_path, ...) |
| 155 |
present_top <- file.exists(files_top) |
| 156 |
|
| 157 |
# Merge them together. Here are the different possible conditions, and the |
| 158 |
# desired result. NULL means to drop that element from the result. |
| 159 |
# |
| 160 |
# files_inst: /inst/A /inst/B /inst/C /inst/D |
| 161 |
# present_inst: T T F F |
| 162 |
# files_top: /A /B /C /D |
| 163 |
# present_top: T F T F |
| 164 |
# result: /inst/A /inst/B /C NULL |
| 165 |
# |
| 166 |
files <- files_top |
| 167 |
files[present_inst] <- files_inst[present_inst] |
| 168 |
# Drop cases where not present in either location |
| 169 |
files <- files[present_inst | present_top] |
| 170 |
if (length(files) == 0) { |
| 171 |
return("") |
| 172 |
} |
| 173 |
# Make sure backslashes are replaced with slashes on Windows |
| 174 |
normalizePath(files, winslash = "/") |
| 175 |
} |
| 176 |
|
| 177 |
# A wrapper for `system.file()`, which caches the package path because |
| 178 |
# `system.file()` can be slow. If a package is not installed, the result won't |
| 179 |
# be cached. |
| 180 |
system_file_cached <- local({ |
| 181 |
pkg_dir_cache <- character() |
| 182 |
|
| 183 |
function(..., package = "base") { |
| 184 |
if (!is.null(names(list(...)))) { |
| 185 |
stop("All arguments other than `package` must be unnamed.") |
| 186 |
} |
| 187 |
|
| 188 |
not_cached <- is.na(match(package, names(pkg_dir_cache))) |
| 189 |
if (not_cached) { |
| 190 |
pkg_dir <- system.file(package = package) |
| 191 |
if (nzchar(pkg_dir)) { |
| 192 |
pkg_dir_cache[[package]] <<- pkg_dir |
| 193 |
} |
| 194 |
} else { |
| 195 |
pkg_dir <- pkg_dir_cache[[package]] |
| 196 |
} |
| 197 |
|
| 198 |
file.path(pkg_dir, ...) |
| 199 |
} |
| 200 |
}) |