Skip to content

Commit 1b71f1d

Browse files
committed
Update staticimports
1 parent 97d3d97 commit 1b71f1d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

R/staticimports.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,19 @@ get_package_version <- function(pkg) {
3030

3131
is_installed <- function(pkg, version = NULL) {
3232
installed <- isNamespaceLoaded(pkg) || nzchar(system_file_cached(package = pkg))
33+
3334
if (is.null(version)) {
3435
return(installed)
3536
}
37+
38+
if (!is.character(version) && !inherits(version, "numeric_version")) {
39+
# Avoid https://bugs.r-project.org/show_bug.cgi?id=18548
40+
alert <- if (identical(Sys.getenv("TESTTHAT"), "true")) stop else warning
41+
alert("`version` must be a character string or a `package_version` or `numeric_version` object.")
42+
43+
version <- numeric_version(sprintf("%0.9g", version))
44+
}
45+
3646
installed && isTRUE(get_package_version(pkg) >= version)
3747
}
3848

@@ -178,11 +188,9 @@ system_file <- function(..., package = "base") {
178188
normalizePath(files, winslash = "/")
179189
}
180190

181-
# A wrapper for `system.file()`, which caches the results, because
182-
# `system.file()` can be slow. Note that because of caching, if
183-
# `system_file_cached()` is called on a package that isn't installed, then the
184-
# package is installed, and then `system_file_cached()` is called again, it will
185-
# still return "".
191+
# A wrapper for `system.file()`, which caches the package path because
192+
# `system.file()` can be slow. If a package is not installed, the result won't
193+
# be cached.
186194
system_file_cached <- local({
187195
pkg_dir_cache <- character()
188196

@@ -194,7 +202,9 @@ system_file_cached <- local({
194202
not_cached <- is.na(match(package, names(pkg_dir_cache)))
195203
if (not_cached) {
196204
pkg_dir <- system.file(package = package)
197-
pkg_dir_cache[[package]] <<- pkg_dir
205+
if (nzchar(pkg_dir)) {
206+
pkg_dir_cache[[package]] <<- pkg_dir
207+
}
198208
} else {
199209
pkg_dir <- pkg_dir_cache[[package]]
200210
}

0 commit comments

Comments
 (0)