Skip to content

Commit

Permalink
Put in shims for scales/#427
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Oct 3, 2024
1 parent 7251f11 commit 0901257
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,8 @@ warn_dots_used <- function(env = caller_env(), call = caller_env()) {
)
}

# TODO: delete shims when {scales} releases >1.3.0.9000
# and bump {scales} version requirements
# Shim for scales/#424
col_mix <- function(a, b, amount = 0.5) {
input <- vec_recycle_common(a = a, b = b, amount = amount)
Expand All @@ -829,10 +831,40 @@ col_mix <- function(a, b, amount = 0.5) {
)
}

# Shim for scales/#427
as_discrete_pal <- function(x, ...) {
if (is.function(x)) {
return(x)
}
pal_manual(x)
}

# Shim for scales/#427
as_continuous_pal <- function(x, ...) {
if (is.function(x)) {
return(x)
}
is_color <- grepl("^#(([[:xdigit:]]{2}){3,4}|([[:xdigit:]]){3,4})$", x) |
x %in% grDevices::colours()
if (all(is_color)) {
colour_ramp(x)
} else {
approxfun(seq(0, 1, length.out = length(x)), x)
}
}

# Replace shims by actual scales function when available
on_load({
if ("col_mix" %in% getNamespaceExports("scales")) {
nse <- getNamespaceExports("scales")
if ("col_mix" %in% nse) {
col_mix <- scales::col_mix
}
if ("as_discrete_pal" %in% nse) {
as_discrete_pal <- scales::as_discrete_pal
}
if ("as_continuous_pal" %in% nse) {
as_continuous_pal <- scales::as_continuous_pal
}
})

# TODO: Replace me if rlang/#1730 gets implemented
Expand Down

0 comments on commit 0901257

Please sign in to comment.