Skip to content

Commit eb8bf83

Browse files
authored
Export is.waive() for extensions to use (#6173)
* Export is.waive() * Add NEWS bullet about is.waive * Rename is.waive to is.waiver
1 parent add05b3 commit eb8bf83

18 files changed

+58
-44
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ export(is.scale)
475475
export(is.stat)
476476
export(is.theme)
477477
export(is.theme_element)
478+
export(is.waiver)
478479
export(label_both)
479480
export(label_bquote)
480481
export(label_context)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* The helper function `is.waiver()` is now exported to help extensions to work
4+
with `waiver()` objects (@arcresu, #6173).
35
* Date(time) scales now throw appropriate errors when `date_breaks`,
46
`date_minor_breaks` or `date_labels` are not strings (@RodDalBen, #5880)
57
* `geom_errorbarh()` is deprecated in favour of

R/axis-secondary.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ is.sec_axis <- function(x) {
129129
}
130130

131131
set_sec_axis <- function(sec.axis, scale) {
132-
if (!is.waive(sec.axis)) {
132+
if (!is.waiver(sec.axis)) {
133133
if (scale$is_discrete()) {
134134
if (!identical(.subset2(sec.axis, "trans"), identity)) {
135135
cli::cli_abort("Discrete secondary axes must have the {.fn identity} transformation.")
@@ -182,9 +182,9 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
182182
if (!is.function(transform)) {
183183
cli::cli_abort("Transformation for secondary axes must be a function.")
184184
}
185-
if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name
185+
if (is.derived(self$name) && !is.waiver(scale$name)) self$name <- scale$name
186186
if (is.derived(self$breaks)) self$breaks <- scale$breaks
187-
if (is.waive(self$breaks)) {
187+
if (is.waiver(self$breaks)) {
188188
if (scale$is_discrete()) {
189189
self$breaks <- scale$get_breaks()
190190
} else {

R/coord-sf.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
108108
x_breaks <- graticule$degree[graticule$type == "E"]
109109
if (is.null(scale_x$labels)) {
110110
x_labels <- rep(NA, length(x_breaks))
111-
} else if (is.waive(scale_x$labels)) {
111+
} else if (is.waiver(scale_x$labels)) {
112112
x_labels <- graticule$degree_label[graticule$type == "E"]
113113
needs_autoparsing[graticule$type == "E"] <- TRUE
114114
} else {
@@ -133,7 +133,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
133133
y_breaks <- graticule$degree[graticule$type == "N"]
134134
if (is.null(scale_y$labels)) {
135135
y_labels <- rep(NA, length(y_breaks))
136-
} else if (is.waive(scale_y$labels)) {
136+
} else if (is.waiver(scale_y$labels)) {
137137
y_labels <- graticule$degree_label[graticule$type == "N"]
138138
needs_autoparsing[graticule$type == "N"] <- TRUE
139139
} else {
@@ -534,7 +534,7 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
534534
label_axes = waiver(), lims_method = "cross",
535535
ndiscr = 100, default = FALSE, clip = "on") {
536536

537-
if (is.waive(label_graticule) && is.waive(label_axes)) {
537+
if (is.waiver(label_graticule) && is.waiver(label_axes)) {
538538
# if both `label_graticule` and `label_axes` are set to waive then we
539539
# use the default of labels on the left and at the bottom
540540
label_graticule <- ""
@@ -620,13 +620,13 @@ sf_breaks <- function(scale_x, scale_y, bbox, crs) {
620620
bbox[is.na(bbox)] <- c(-180, -90, 180, 90)[is.na(bbox)]
621621
}
622622

623-
if (!(is.waive(scale_x$breaks) && is.null(scale_x$n.breaks))) {
623+
if (!(is.waiver(scale_x$breaks) && is.null(scale_x$n.breaks))) {
624624
x_breaks <- scale_x$get_breaks(limits = bbox[c(1, 3)])
625625
finite <- is.finite(x_breaks)
626626
x_breaks <- if (any(finite)) x_breaks[finite] else NULL
627627
}
628628

629-
if (!(is.waive(scale_y$breaks) && is.null(scale_y$n.breaks))) {
629+
if (!(is.waiver(scale_y$breaks) && is.null(scale_y$n.breaks))) {
630630
y_breaks <- scale_y$get_breaks(limits = bbox[c(2, 4)])
631631
finite <- is.finite(y_breaks)
632632
y_breaks <- if (any(finite)) y_breaks[finite] else NULL

R/facet-null.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ FacetNull <- ggproto("FacetNull", Facet,
2727
layout_null()
2828
},
2929
map_data = function(data, layout, params) {
30-
# Need the is.waive check for special case where no data, but aesthetics
30+
# Need the is.waiver check for special case where no data, but aesthetics
3131
# are mapped to vectors
32-
if (is.waive(data))
32+
if (is.waiver(data))
3333
return(data_frame0(PANEL = factor()))
3434

3535
if (empty(data))

R/guide-axis-theta.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ GuideAxisTheta <- ggproto(
200200
}
201201

202202
# Resolve text angle
203-
if (is.waive(params$angle) || is.null(params$angle)) {
203+
if (is.waiver(params$angle) || is.null(params$angle)) {
204204
angle <- elements$text$angle
205205
} else {
206206
angle <- flip_text_angle(params$angle - rad2deg(key$theta))
@@ -276,7 +276,7 @@ GuideAxisTheta <- ggproto(
276276
}
277277

278278
# Resolve text angle
279-
if (is.waive(params$angle %||% waiver())) {
279+
if (is.waiver(params$angle %||% waiver())) {
280280
angle <- elements$text$angle
281281
} else {
282282
angle <- flip_text_angle(params$angle - rad2deg(key$theta))

R/guide-custom.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ GuideCustom <- ggproto(
9696
# Render title
9797
params <- replace_null(params, position = position, direction = direction)
9898
elems <- GuideLegend$setup_elements(params, self$elements, theme)
99-
if (!is.waive(params$title) && !is.null(params$title)) {
99+
if (!is.waiver(params$title) && !is.null(params$title)) {
100100
title <- self$build_title(params$title, elems, params)
101101
} else {
102102
title <- zeroGrob()

R/labels.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
152152
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
153153
.ignore_empty = "all")
154154

155-
is_waive <- vapply(args, is.waive, logical(1))
155+
is_waive <- vapply(args, is.waiver, logical(1))
156156
args <- args[!is_waive]
157157
# remove duplicated arguments
158158
args <- args[!duplicated(names(args))]

R/layer.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ Layer <- ggproto("Layer", NULL,
253253
},
254254

255255
layer_data = function(self, plot_data) {
256-
if (is.waive(self$data)) {
256+
if (is.waiver(self$data)) {
257257
data <- plot_data
258258
} else if (is.function(self$data)) {
259259
data <- self$data(plot_data)
@@ -263,7 +263,7 @@ Layer <- ggproto("Layer", NULL,
263263
} else {
264264
data <- self$data
265265
}
266-
if (is.null(data) || is.waive(data)) data else unrowname(data)
266+
if (is.null(data) || is.waiver(data)) data else unrowname(data)
267267
},
268268

269269
# hook to allow a layer access to the final layer data

R/layout.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Layout <- ggproto("Layout", NULL,
283283
} else {
284284
switch(label, x = ".bottom", y = ".right")
285285
}
286-
if (is.null(labels[[label]][[i]]) || is.waive(labels[[label]][[i]]))
286+
if (is.null(labels[[label]][[i]]) || is.waiver(labels[[label]][[i]]))
287287
return(zeroGrob())
288288

289289
element_render(

R/scale-.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
753753
# don't support conversion to numeric (#5304)
754754
if (zero_range(as.numeric(transformation$transform(limits)))) {
755755
breaks <- limits[1]
756-
} else if (is.waive(self$breaks)) {
756+
} else if (is.waiver(self$breaks)) {
757757
if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) {
758758
breaks <- transformation$breaks(limits, self$n.breaks)
759759
} else {
@@ -795,7 +795,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
795795
b <- b[is.finite(b)]
796796

797797
transformation <- self$get_transformation()
798-
if (is.waive(self$minor_breaks)) {
798+
if (is.waiver(self$minor_breaks)) {
799799
if (is.null(b)) {
800800
breaks <- NULL
801801
} else {
@@ -842,7 +842,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
842842
)
843843
}
844844

845-
if (is.waive(self$labels)) {
845+
if (is.waiver(self$labels)) {
846846
labels <- transformation$format(breaks)
847847
} else if (is.function(self$labels)) {
848848
labels <- self$labels(breaks)
@@ -1022,7 +1022,7 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
10221022
)
10231023
}
10241024

1025-
if (is.waive(self$breaks)) {
1025+
if (is.waiver(self$breaks)) {
10261026
breaks <- limits
10271027
} else if (is.function(self$breaks)) {
10281028
breaks <- self$breaks(limits)
@@ -1084,7 +1084,7 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
10841084
)
10851085
}
10861086

1087-
if (is.waive(self$labels)) {
1087+
if (is.waiver(self$labels)) {
10881088
if (is.numeric(breaks)) {
10891089
# Only format numbers, because on Windows, format messes up encoding
10901090
format(breaks, justify = "none")
@@ -1244,7 +1244,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
12441244
"Invalid {.arg breaks} specification. Use {.code NULL}, not {.code NA}.",
12451245
call = self$call
12461246
)
1247-
} else if (is.waive(self$breaks)) {
1247+
} else if (is.waiver(self$breaks)) {
12481248
if (self$nice.breaks) {
12491249
if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) {
12501250
breaks <- transformation$breaks(limits, n = self$n.breaks)
@@ -1334,7 +1334,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
13341334
"Invalid {.arg labels} specification. Use {.code NULL}, not {.code NA}.",
13351335
call = self$call
13361336
)
1337-
} else if (is.waive(self$labels)) {
1337+
} else if (is.waiver(self$labels)) {
13381338
labels <- transformation$format(breaks)
13391339
} else if (is.function(self$labels)) {
13401340
labels <- self$labels(breaks)

R/scale-continuous.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,21 @@ ScaleContinuousPosition <- ggproto("ScaleContinuousPosition", ScaleContinuous,
146146
},
147147
break_info = function(self, range = NULL) {
148148
breaks <- ggproto_parent(ScaleContinuous, self)$break_info(range)
149-
if (!(is.waive(self$secondary.axis) || self$secondary.axis$empty())) {
149+
if (!(is.waiver(self$secondary.axis) || self$secondary.axis$empty())) {
150150
self$secondary.axis$init(self)
151151
breaks <- c(breaks, self$secondary.axis$break_info(breaks$range, self))
152152
}
153153
breaks
154154
},
155155
sec_name = function(self) {
156-
if (is.waive(self$secondary.axis)) {
156+
if (is.waiver(self$secondary.axis)) {
157157
waiver()
158158
} else {
159159
self$secondary.axis$name
160160
}
161161
},
162162
make_sec_title = function(self, title) {
163-
if (!is.waive(self$secondary.axis)) {
163+
if (!is.waiver(self$secondary.axis)) {
164164
self$secondary.axis$make_title(title)
165165
} else {
166166
ggproto_parent(ScaleContinuous, self)$make_sec_title(title)

R/scale-date.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,15 @@ datetime_scale <- function(aesthetics, transform, trans = deprecated(),
302302
if (is.character(breaks)) breaks <- breaks_width(breaks)
303303
if (is.character(minor_breaks)) minor_breaks <- breaks_width(minor_breaks)
304304

305-
if (!is.waive(date_breaks)) {
305+
if (!is.waiver(date_breaks)) {
306306
check_string(date_breaks)
307307
breaks <- breaks_width(date_breaks)
308308
}
309-
if (!is.waive(date_minor_breaks)) {
309+
if (!is.waiver(date_minor_breaks)) {
310310
check_string(date_minor_breaks)
311311
minor_breaks <- breaks_width(date_minor_breaks)
312312
}
313-
if (!is.waive(date_labels)) {
313+
if (!is.waiver(date_labels)) {
314314
check_string(date_labels)
315315
labels <- function(self, x) {
316316
tz <- self$timezone %||% "UTC"
@@ -379,21 +379,21 @@ ScaleContinuousDatetime <- ggproto("ScaleContinuousDatetime", ScaleContinuous,
379379
},
380380
break_info = function(self, range = NULL) {
381381
breaks <- ggproto_parent(ScaleContinuous, self)$break_info(range)
382-
if (!(is.waive(self$secondary.axis) || self$secondary.axis$empty())) {
382+
if (!(is.waiver(self$secondary.axis) || self$secondary.axis$empty())) {
383383
self$secondary.axis$init(self)
384384
breaks <- c(breaks, self$secondary.axis$break_info(breaks$range, self))
385385
}
386386
breaks
387387
},
388388
sec_name = function(self) {
389-
if (is.waive(self$secondary.axis)) {
389+
if (is.waiver(self$secondary.axis)) {
390390
waiver()
391391
} else {
392392
self$secondary.axis$name
393393
}
394394
},
395395
make_sec_title = function(self, title) {
396-
if (!is.waive(self$secondary.axis)) {
396+
if (!is.waiver(self$secondary.axis)) {
397397
self$secondary.axis$make_title(title)
398398
} else {
399399
ggproto_parent(ScaleContinuous, self)$make_sec_title(title)
@@ -430,21 +430,21 @@ ScaleContinuousDate <- ggproto("ScaleContinuousDate", ScaleContinuous,
430430
},
431431
break_info = function(self, range = NULL) {
432432
breaks <- ggproto_parent(ScaleContinuous, self)$break_info(range)
433-
if (!(is.waive(self$secondary.axis) || self$secondary.axis$empty())) {
433+
if (!(is.waiver(self$secondary.axis) || self$secondary.axis$empty())) {
434434
self$secondary.axis$init(self)
435435
breaks <- c(breaks, self$secondary.axis$break_info(breaks$range, self))
436436
}
437437
breaks
438438
},
439439
sec_name = function(self) {
440-
if (is.waive(self$secondary.axis)) {
440+
if (is.waiver(self$secondary.axis)) {
441441
waiver()
442442
} else {
443443
self$secondary.axis$name
444444
}
445445
},
446446
make_sec_title = function(self, title) {
447-
if (!is.waive(self$secondary.axis)) {
447+
if (!is.waiver(self$secondary.axis)) {
448448
self$secondary.axis$make_title(title)
449449
} else {
450450
ggproto_parent(ScaleContinuous, self)$make_sec_title(title)

R/scale-discrete-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
170170
},
171171

172172
sec_name = function(self) {
173-
if (is.waive(self$secondary.axis)) {
173+
if (is.waiver(self$secondary.axis)) {
174174
waiver()
175175
} else {
176176
self$secondary.axis$name

R/scale-manual.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(),
171171
}
172172

173173
# order values according to breaks
174-
if (is.vector(values) && is.null(names(values)) && !is.waive(breaks) &&
174+
if (is.vector(values) && is.null(names(values)) && !is.waiver(breaks) &&
175175
!is.null(breaks) && !is.function(breaks)) {
176176
if (length(breaks) <= length(values)) {
177177
names(values) <- breaks

R/scale-view.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ view_scale_primary <- function(scale, limits = scale$get_limits(),
4545
view_scale_secondary <- function(scale, limits = scale$get_limits(),
4646
continuous_range = scale$dimension(limits = limits)) {
4747

48-
if (is.null(scale$secondary.axis) || is.waive(scale$secondary.axis) || scale$secondary.axis$empty()) {
48+
if (is.null(scale$secondary.axis) || is.waiver(scale$secondary.axis) || scale$secondary.axis$empty()) {
4949
# if there is no second axis, return the primary scale with no guide
5050
# this guide can be overridden using guides()
5151
primary_scale <- view_scale_primary(scale, limits, continuous_range)

R/utilities.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ scales::alpha
1212
}
1313

1414
"%|W|%" <- function(a, b) {
15-
if (!is.waive(a)) a else b
15+
if (!is.waiver(a)) a else b
1616
}
1717

1818
# Check required aesthetics are present
@@ -182,13 +182,17 @@ should_stop <- function(expr) {
182182
#' A waiver is a "flag" object, similar to `NULL`, that indicates the
183183
#' calling function should just use the default value. It is used in certain
184184
#' functions to distinguish between displaying nothing (`NULL`) and
185-
#' displaying a default value calculated elsewhere (`waiver()`)
185+
#' displaying a default value calculated elsewhere (`waiver()`).
186+
#' `is.waiver()` reports whether an object is a waiver.
186187
#'
187188
#' @export
188189
#' @keywords internal
189190
waiver <- function() structure(list(), class = "waiver")
190191

191-
is.waive <- function(x) inherits(x, "waiver")
192+
#' @param x An object to test
193+
#' @export
194+
#' @rdname waiver
195+
is.waiver <- function(x) inherits(x, "waiver")
192196

193197
pal_binned <- function(palette) {
194198
function(x) {
@@ -266,7 +270,7 @@ snake_class <- function(x) {
266270
}
267271

268272
empty <- function(df) {
269-
is.null(df) || nrow(df) == 0 || ncol(df) == 0 || is.waive(df)
273+
is.null(df) || nrow(df) == 0 || ncol(df) == 0 || is.waiver(df)
270274
}
271275

272276
is.discrete <- function(x) {

man/waiver.Rd

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)