Skip to content

Commit 1dd3c27

Browse files
committed
scales have fallback palettes
1 parent 6a447ba commit 1dd3c27

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

R/scale-.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam
107107
oob = censor, expand = waiver(), na.value = NA,
108108
transform = "identity", trans = deprecated(),
109109
guide = "legend", position = "left",
110+
fallback.palette = NULL,
110111
call = caller_call(),
111112
super = ScaleContinuous) {
112113
call <- call %||% current_call()
@@ -121,6 +122,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam
121122
aesthetics <- standardise_aes_names(aesthetics)
122123

123124
check_breaks_labels(breaks, labels, call = call)
125+
check_fallback_palette(palette, fallback.palette, call = call)
124126

125127
position <- arg_match0(position, c("left", "right", "top", "bottom"))
126128

@@ -152,6 +154,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam
152154

153155
aesthetics = aesthetics,
154156
palette = palette,
157+
fallback_palette = fallback.palette,
155158

156159
range = ContinuousRange$new(),
157160
limits = limits,
@@ -211,6 +214,7 @@ discrete_scale <- function(aesthetics, scale_name = deprecated(), palette, name
211214
labels = waiver(), limits = NULL, expand = waiver(),
212215
na.translate = TRUE, na.value = NA, drop = TRUE,
213216
guide = "legend", position = "left",
217+
fallback.palette = NULL,
214218
call = caller_call(),
215219
super = ScaleDiscrete) {
216220
call <- call %||% current_call()
@@ -221,6 +225,7 @@ discrete_scale <- function(aesthetics, scale_name = deprecated(), palette, name
221225
aesthetics <- standardise_aes_names(aesthetics)
222226

223227
check_breaks_labels(breaks, labels, call = call)
228+
check_fallback_palette(palette, fallback.palette, call = call)
224229

225230
# Convert formula input to function if appropriate
226231
limits <- allow_lambda(limits)
@@ -251,6 +256,7 @@ discrete_scale <- function(aesthetics, scale_name = deprecated(), palette, name
251256

252257
aesthetics = aesthetics,
253258
palette = palette,
259+
fallback_palette = fallback.palette,
254260

255261
range = DiscreteRange$new(),
256262
limits = limits,
@@ -303,6 +309,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name =
303309
right = TRUE, transform = "identity",
304310
trans = deprecated(), show.limits = FALSE,
305311
guide = "bins", position = "left",
312+
fallback.palette = NULL,
306313
call = caller_call(),
307314
super = ScaleBinned) {
308315
if (lifecycle::is_present(scale_name)) {
@@ -318,6 +325,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name =
318325
aesthetics <- standardise_aes_names(aesthetics)
319326

320327
check_breaks_labels(breaks, labels, call = call)
328+
check_fallback_palette(palette, fallback.palette, call = call)
321329

322330
position <- arg_match0(position, c("left", "right", "top", "bottom"))
323331

@@ -346,6 +354,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name =
346354

347355
aesthetics = aesthetics,
348356
palette = palette,
357+
fallback_palette = fallback.palette,
349358

350359
range = ContinuousRange$new(),
351360
limits = limits,
@@ -1793,6 +1802,15 @@ check_continuous_limits <- function(limits, ...,
17931802
check_length(limits, 2L, arg = arg, call = call)
17941803
}
17951804

1805+
check_fallback_palette <- function(pal, fallback, call = caller_env()) {
1806+
if (!is.null(pal) || is.function(fallback)) {
1807+
return(invisible)
1808+
}
1809+
cli::cli_abort(
1810+
"When {.code palette = NULL}, the {.arg fallback.palette} must be defined."
1811+
)
1812+
}
1813+
17961814
allow_lambda <- function(x) {
17971815
# we check the 'call' class to prevent interpreting `bquote()` calls as a function
17981816
if (is_formula(x, lhs = FALSE) && !inherits(x, "call")) as_function(x) else x

0 commit comments

Comments
 (0)