Skip to content

Commit 0901257

Browse files
committed
Put in shims for scales/#427
1 parent 7251f11 commit 0901257

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

R/utilities.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ warn_dots_used <- function(env = caller_env(), call = caller_env()) {
817817
)
818818
}
819819

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

834+
# Shim for scales/#427
835+
as_discrete_pal <- function(x, ...) {
836+
if (is.function(x)) {
837+
return(x)
838+
}
839+
pal_manual(x)
840+
}
841+
842+
# Shim for scales/#427
843+
as_continuous_pal <- function(x, ...) {
844+
if (is.function(x)) {
845+
return(x)
846+
}
847+
is_color <- grepl("^#(([[:xdigit:]]{2}){3,4}|([[:xdigit:]]){3,4})$", x) |
848+
x %in% grDevices::colours()
849+
if (all(is_color)) {
850+
colour_ramp(x)
851+
} else {
852+
approxfun(seq(0, 1, length.out = length(x)), x)
853+
}
854+
}
855+
856+
# Replace shims by actual scales function when available
832857
on_load({
833-
if ("col_mix" %in% getNamespaceExports("scales")) {
858+
nse <- getNamespaceExports("scales")
859+
if ("col_mix" %in% nse) {
834860
col_mix <- scales::col_mix
835861
}
862+
if ("as_discrete_pal" %in% nse) {
863+
as_discrete_pal <- scales::as_discrete_pal
864+
}
865+
if ("as_continuous_pal" %in% nse) {
866+
as_continuous_pal <- scales::as_continuous_pal
867+
}
836868
})
837869

838870
# TODO: Replace me if rlang/#1730 gets implemented

0 commit comments

Comments
 (0)