-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathutils.R
104 lines (90 loc) · 2.67 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# @staticimports pkg:staticimports
# is_installed get_package_version system_file
# register_upgrade_message
# %||% is_string
switch_version <- function(version, five = default, four = default, three = default, default = NULL) {
if (is_bs_theme(version)) {
version <- theme_version(version)
}
version <- as.character(version)
if (isTRUE(version %in% c("4-3", "4+3"))) {
warning("Version ", version, " has been renamed to 4. Please use 4 instead")
version <- "4"
}
switch(
version, `5` = five, `4` = four, `3` = three,
stop("Didn't recognize Bootstrap version: ", version, call. = FALSE)
)
}
get_exact_version <- function(version) {
switch_version(version, five = version_bs5, four = version_bs4, three = version_bs3)
}
lib_file <- function(...) {
files <- system_file("lib", ..., package = "bslib")
files_found <- files != ""
if (all(files_found)) return(files)
files_not_found <- file.path(...)[!files_found]
stop(
"bslib file not found: '", files_not_found, "'",
call. = FALSE
)
}
is_shiny_app <- function() {
# Make sure to not load shiny as a side-effect of calling this function.
isNamespaceLoaded("shiny") && shiny::isRunning()
}
is_hosted_app <- function() {
nzchar(Sys.getenv("SHINY_SERVER_VERSION")) && is_shiny_app()
}
is_shiny_runtime <- function() {
isTRUE(grepl("^shiny", knitr::opts_knit$get("rmarkdown.runtime")))
}
register_runtime_package_check <- function(feature, pkg, version) {
msg <- sprintf(
"%s is designed to work with %s %s or higher",
feature, pkg, version
)
if (isNamespaceLoaded(pkg) && !is_installed(pkg, version)) {
warning(msg, call. = FALSE)
}
setHook(
packageEvent(pkg, "onLoad"),
function(...) {
if (!is_installed(pkg, version)) warning(msg, call. = FALSE)
}
)
}
add_class <- function(x, y) {
class(x) <- unique(c(y, oldClass(x)))
x
}
dropNulls <- function(x) {
x[!vapply(x, is.null, FUN.VALUE=logical(1))]
}
names2 <- function(x) {
names(x) %||% rep.int("", length(x))
}
#' Rename a named list
#'
#' @param x a named list to be renamed
#' @param nms a named character vector defining the renaming
#' @noRd
#' @examples
#' rename2(list(a=1, b=3, c=4, a=2), b="z", f="w", a="y")
#' #> list(y = 1, z = 3, c = 4, y = 2)
#' rename2(c("a", "b", "c", "a"), b="z", f="w", a="y")
#' #> c("y", "z", "c", "y")
rename2 <- function(x, ...) {
defs <- rlang::list2(...)
nms <- names(x) %||% as.character(x)
matches <- intersect(nms, names(defs))
map <- match(nms, names(defs))
mapidxNA <- is.na(map)
replacement <- as.character(defs)[map[!mapidxNA]]
if (is.null(names(x))) {
x[!mapidxNA] <- replacement
} else {
names(x)[!mapidxNA] <- replacement
}
x
}