Skip to content

Commit 660aad2

Browse files
authored
Move error signalling to rlang::abort() (#3526)
1 parent ee3cf49 commit 660aad2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+450
-462
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Depends:
2121
R (>= 3.2)
2222
Imports:
2323
digest,
24+
glue,
2425
grDevices,
2526
grid,
2627
gtable (>= 0.1.1),

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,8 @@ import(grid)
643643
import(gtable)
644644
import(rlang)
645645
import(scales)
646+
importFrom(glue,glue)
647+
importFrom(glue,glue_collapse)
646648
importFrom(stats,setNames)
647649
importFrom(tibble,tibble)
648650
importFrom(utils,.DollarNames)

R/aes-evaluation.r

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ is_calculated <- function(x) {
121121
} else if (is.pairlist(x)) {
122122
FALSE
123123
} else {
124-
stop("Unknown input:", class(x)[1])
124+
abort(glue("Unknown input: {class(x)[1]}"))
125125
}
126126
}
127127
is_scaled <- function(x) {
@@ -162,7 +162,7 @@ strip_dots <- function(expr) {
162162
# For list of aesthetics
163163
lapply(expr, strip_dots)
164164
} else {
165-
stop("Unknown input:", class(expr)[1])
165+
abort(glue("Unknown input: {class(expr)[1]}"))
166166
}
167167
}
168168

R/aes.r

+9-14
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ new_aesthetic <- function(x, env = globalenv()) {
102102
x
103103
}
104104
new_aes <- function(x, env = globalenv()) {
105-
stopifnot(is.list(x))
105+
if (!is.list(x)) {
106+
abort("`x` must be a list")
107+
}
106108
x <- lapply(x, new_aesthetic, env = env)
107109
structure(x, class = "uneval")
108110
}
@@ -168,9 +170,7 @@ rename_aes <- function(x) {
168170
duplicated_names <- names(x)[duplicated(names(x))]
169171
if (length(duplicated_names) > 0L) {
170172
duplicated_message <- paste0(unique(duplicated_names), collapse = ", ")
171-
warning(
172-
"Duplicated aesthetics after name standardisation: ", duplicated_message, call. = FALSE
173-
)
173+
warn(glue("Duplicated aesthetics after name standardisation: {duplicated_message}"))
174174
}
175175
x
176176
}
@@ -270,8 +270,7 @@ aes_ <- function(x, y, ...) {
270270
} else if (is.call(x) || is.name(x) || is.atomic(x)) {
271271
new_aesthetic(x, caller_env)
272272
} else {
273-
stop("Aesthetic must be a one-sided formula, call, name, or constant.",
274-
call. = FALSE)
273+
abort("Aesthetic must be a one-sided formula, call, name, or constant.")
275274
}
276275
}
277276
mapping <- lapply(mapping, as_quosure_aes)
@@ -327,11 +326,11 @@ aes_all <- function(vars) {
327326
#' @keywords internal
328327
#' @export
329328
aes_auto <- function(data = NULL, ...) {
330-
warning("aes_auto() is deprecated", call. = FALSE)
329+
warn("aes_auto() is deprecated")
331330

332331
# detect names of data
333332
if (is.null(data)) {
334-
stop("aes_auto requires data.frame or names of data.frame.")
333+
abort("aes_auto requires data.frame or names of data.frame.")
335334
} else if (is.data.frame(data)) {
336335
vars <- names(data)
337336
} else {
@@ -380,11 +379,7 @@ warn_for_aes_extract_usage_expr <- function(x, data, env = emptyenv()) {
380379
if (is_call(x, "[[") || is_call(x, "$")) {
381380
if (extract_target_is_likely_data(x, data, env)) {
382381
good_usage <- alternative_aes_extract_usage(x)
383-
warning(
384-
"Use of `", format(x), "` is discouraged. ",
385-
"Use `", good_usage, "` instead.",
386-
call. = FALSE
387-
)
382+
warn(glue("Use of `{format(x)}` is discouraged. Use `{good_usage}` instead."))
388383
}
389384
} else if (is.call(x)) {
390385
lapply(x, warn_for_aes_extract_usage_expr, data, env)
@@ -398,7 +393,7 @@ alternative_aes_extract_usage <- function(x) {
398393
} else if (is_call(x, "$")) {
399394
as.character(x[[3]])
400395
} else {
401-
stop("Don't know how to get alternative usage for `", format(x), "`", call. = FALSE)
396+
abort(glue("Don't know how to get alternative usage for `{format(x)}`"))
402397
}
403398
}
404399

R/annotation-custom.r

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ GeomCustomAnn <- ggproto("GeomCustomAnn", Geom,
7171
draw_panel = function(data, panel_params, coord, grob, xmin, xmax,
7272
ymin, ymax) {
7373
if (!inherits(coord, "CoordCartesian")) {
74-
stop("annotation_custom only works with Cartesian coordinates",
75-
call. = FALSE)
74+
abort("annotation_custom only works with Cartesian coordinates")
7675
}
7776
corners <- new_data_frame(list(x = c(xmin, xmax), y = c(ymin, ymax)), n = 2)
7877
data <- coord$transform(corners, panel_params)

R/annotation-map.r

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ NULL
3131
#' }
3232
annotation_map <- function(map, ...) {
3333
# Get map input into correct form
34-
stopifnot(is.data.frame(map))
34+
if (!is.data.frame(map)) {
35+
abort("`map` must be a data.frame")
36+
}
3537
if (!is.null(map$lat)) map$y <- map$lat
3638
if (!is.null(map$long)) map$x <- map$long
3739
if (!is.null(map$region)) map$id <- map$region
38-
stopifnot(all(c("x", "y", "id") %in% names(map)))
40+
if (!all(c("x", "y", "id") %in% names(map))) {
41+
abort("`map`must have the columns `x`, `y`, and `id`")
42+
}
3943

4044
layer(
4145
data = dummy_data(),

R/annotation-raster.r

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ GeomRasterAnn <- ggproto("GeomRasterAnn", Geom,
7373
draw_panel = function(data, panel_params, coord, raster, xmin, xmax,
7474
ymin, ymax, interpolate = FALSE) {
7575
if (!inherits(coord, "CoordCartesian")) {
76-
stop("annotation_raster only works with Cartesian coordinates",
77-
call. = FALSE)
76+
abort("annotation_raster only works with Cartesian coordinates")
7877
}
7978
corners <- new_data_frame(list(x = c(xmin, xmax), y = c(ymin, ymax)), n = 2)
8079
data <- coord$transform(corners, panel_params)

R/annotation.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
5858
bad <- lengths != 1L
5959
details <- paste(names(aesthetics)[bad], " (", lengths[bad], ")",
6060
sep = "", collapse = ", ")
61-
stop("Unequal parameter lengths: ", details, call. = FALSE)
61+
abort(glue("Unequal parameter lengths: {details}"))
6262
}
6363

6464
data <- new_data_frame(position, n = n)

R/autolayer.r

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ autolayer <- function(object, ...) {
1515

1616
#' @export
1717
autolayer.default <- function(object, ...) {
18-
stop("Objects of type ", paste(class(object), collapse = "/"),
19-
" not supported by autolayer.", call. = FALSE)
18+
abort(glue(
19+
"Objects of type ",
20+
glue_collapse(class(object), "/"),
21+
" not supported by autolayer."
22+
))
2023
}

R/autoplot.r

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ autoplot <- function(object, ...) {
1515

1616
#' @export
1717
autoplot.default <- function(object, ...) {
18-
stop("Objects of type ", paste(class(object), collapse = "/"),
19-
" not supported by autoplot.", call. = FALSE)
18+
abort(glue(
19+
"Objects of type ",
20+
glue_collapse(class(object), "/"),
21+
" not supported by autoplot."
22+
))
2023
}
2124

R/axis-secondary.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ is.sec_axis <- function(x) {
110110
set_sec_axis <- function(sec.axis, scale) {
111111
if (!is.waive(sec.axis)) {
112112
if (is.formula(sec.axis)) sec.axis <- sec_axis(sec.axis)
113-
if (!is.sec_axis(sec.axis)) stop("Secondary axes must be specified using 'sec_axis()'")
113+
if (!is.sec_axis(sec.axis)) abort("Secondary axes must be specified using 'sec_axis()'")
114114
scale$secondary.axis <- sec.axis
115115
}
116116
return(scale)
@@ -148,7 +148,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
148148
# Inherit settings from the primary axis/scale
149149
init = function(self, scale) {
150150
if (self$empty()) return()
151-
if (!is.function(self$trans)) stop("transformation for secondary axes must be a function", call. = FALSE)
151+
if (!is.function(self$trans)) abort("transformation for secondary axes must be a function")
152152
if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name
153153
if (is.derived(self$breaks)) self$breaks <- scale$breaks
154154
if (is.waive(self$breaks)) self$breaks <- scale$trans$breaks
@@ -170,7 +170,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
170170

171171
# Test for monotonicity
172172
if (length(unique(sign(diff(full_range)))) != 1)
173-
stop("transformation for secondary axes must be monotonic")
173+
abort("transformation for secondary axes must be monotonic")
174174
},
175175

176176
break_info = function(self, range, scale) {

R/bench.r

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
benchplot <- function(x) {
1616
x <- enquo(x)
1717
construct <- system.time(x <- eval_tidy(x))
18-
stopifnot(inherits(x, "ggplot"))
18+
if (!inherits(x, "ggplot")) {
19+
abort("`x` must be a ggplot object")
20+
}
1921

2022
build <- system.time(data <- ggplot_build(x))
2123
render <- system.time(grob <- ggplot_gtable(data))

R/bin.R

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
bins <- function(breaks, closed = c("right", "left"),
22
fuzz = 1e-08 * stats::median(diff(breaks))) {
3-
stopifnot(is.numeric(breaks))
3+
if (!is.numeric(breaks)) abort("`breaks` must be a numeric vector")
44
closed <- match.arg(closed)
55

66
breaks <- sort(breaks)
@@ -50,18 +50,18 @@ bin_breaks <- function(breaks, closed = c("right", "left")) {
5050

5151
bin_breaks_width <- function(x_range, width = NULL, center = NULL,
5252
boundary = NULL, closed = c("right", "left")) {
53-
stopifnot(length(x_range) == 2)
53+
if (length(x_range) != 2) abort("`x_range` must have two elements")
5454

5555
# if (length(x_range) == 0) {
5656
# return(bin_params(numeric()))
5757
# }
58-
stopifnot(is.numeric(width), length(width) == 1)
58+
if (!(is.numeric(width) && length(width) == 1)) abort("`width` must be a numeric scalar")
5959
if (width <= 0) {
60-
stop("`binwidth` must be positive", call. = FALSE)
60+
abort("`binwidth` must be positive")
6161
}
6262

6363
if (!is.null(boundary) && !is.null(center)) {
64-
stop("Only one of 'boundary' and 'center' may be specified.")
64+
abort("Only one of 'boundary' and 'center' may be specified.")
6565
} else if (is.null(boundary)) {
6666
if (is.null(center)) {
6767
# If neither edge nor center given, compute both using tile layer's
@@ -92,19 +92,19 @@ bin_breaks_width <- function(x_range, width = NULL, center = NULL,
9292
# single break (see issue #3606). We fix this by adding a second break.
9393
breaks <- c(breaks, breaks + width)
9494
} else if (length(breaks) > 1e6) {
95-
stop("The number of histogram bins must be less than 1,000,000.\nDid you make `binwidth` too small?", call. = FALSE)
95+
abort("The number of histogram bins must be less than 1,000,000.\nDid you make `binwidth` too small?")
9696
}
9797

9898
bin_breaks(breaks, closed = closed)
9999
}
100100

101101
bin_breaks_bins <- function(x_range, bins = 30, center = NULL,
102102
boundary = NULL, closed = c("right", "left")) {
103-
stopifnot(length(x_range) == 2)
103+
if (length(x_range) != 2) abort("`x_range` must have two elements")
104104

105105
bins <- as.integer(bins)
106106
if (bins < 1) {
107-
stop("Need at least one bin.", call. = FALSE)
107+
abort("Need at least one bin.")
108108
} else if (zero_range(x_range)) {
109109
# 0.1 is the same width as the expansion `default_expansion()` gives for 0-width data
110110
width <- 0.1
@@ -123,7 +123,7 @@ bin_breaks_bins <- function(x_range, bins = 30, center = NULL,
123123
# Compute bins ------------------------------------------------------------
124124

125125
bin_vector <- function(x, bins, weight = NULL, pad = FALSE) {
126-
stopifnot(is_bins(bins))
126+
if (!is_bins(bins)) abort("`bins` must be a ggplot2_bins object")
127127

128128
if (all(is.na(x))) {
129129
return(bin_out(length(x), NA, NA, xmin = NA, xmax = NA))

R/compat-plyr.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ unrowname <- function(x) {
1919
} else if (is.matrix(x)) {
2020
dimnames(x)[1] <- list(NULL)
2121
} else {
22-
stop("Can only remove rownames from data.frame and matrix objects", call. = FALSE)
22+
abort("Can only remove rownames from data.frame and matrix objects")
2323
}
2424
x
2525
}
@@ -193,7 +193,7 @@ revalue <- function(x, replace) {
193193
lev[match(names(replace), lev)] <- replace
194194
levels(x) <- lev
195195
} else if (!is.null(x)) {
196-
stop("x is not a factor or character vector", call. = FALSE)
196+
abort("x is not a factor or character vector")
197197
}
198198
x
199199
}
@@ -239,14 +239,14 @@ as.quoted <- function(x, env = parent.frame()) {
239239
} else if (is.call(x)) {
240240
as.list(x)[-1]
241241
} else {
242-
stop("Only knows how to quote characters, calls, and formula", call. = FALSE)
242+
abort("Only knows how to quote characters, calls, and formula")
243243
}
244244
attributes(x) <- list(env = env, class = 'quoted')
245245
x
246246
}
247247
# round a number to a given precision
248248
round_any <- function(x, accuracy, f = round) {
249-
if (!is.numeric(x)) stop("x must be numeric", call. = FALSE)
249+
if (!is.numeric(x)) abort("`x` must be numeric")
250250
f(x/accuracy) * accuracy
251251
}
252252
#' Bind data frames together by common column names

R/coord-.r

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,27 @@ Coord <- ggproto("Coord",
6464
render_fg = function(panel_params, theme) element_render(theme, "panel.border"),
6565

6666
render_bg = function(panel_params, theme) {
67-
stop("Not implemented", call. = FALSE)
67+
abort("Not implemented")
6868
},
6969

7070
render_axis_h = function(panel_params, theme) {
71-
stop("Not implemented", call. = FALSE)
71+
abort("Not implemented")
7272
},
7373

7474
render_axis_v = function(panel_params, theme) {
75-
stop("Not implemented", call. = FALSE)
75+
abort("Not implemented")
7676
},
7777

7878
# transform range given in transformed coordinates
7979
# back into range in given in (possibly scale-transformed)
8080
# data coordinates
8181
backtransform_range = function(self, panel_params) {
82-
stop("Not implemented", call. = FALSE)
82+
abort("Not implemented")
8383
},
8484

8585
# return range stored in panel_params
8686
range = function(panel_params) {
87-
stop("Not implemented", call. = FALSE)
87+
abort("Not implemented")
8888
},
8989

9090
setup_panel_params = function(scale_x, scale_y, params = list()) {

R/coord-sf.R

+5-14
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
8484
}
8585

8686
if (length(x_labels) != length(x_breaks)) {
87-
stop("Breaks and labels along x direction are different lengths", call. = FALSE)
87+
abort("Breaks and labels along x direction are different lengths")
8888
}
8989
graticule$degree_label[graticule$type == "E"] <- x_labels
9090

@@ -109,7 +109,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
109109
}
110110

111111
if (length(y_labels) != length(y_breaks)) {
112-
stop("Breaks and labels along y direction are different lengths", call. = FALSE)
112+
abort("Breaks and labels along y direction are different lengths")
113113
}
114114
graticule$degree_label[graticule$type == "N"] <- y_labels
115115

@@ -167,10 +167,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
167167

168168
backtransform_range = function(panel_params) {
169169
# this does not actually return backtransformed ranges in the general case, needs fixing
170-
warning(
171-
"range backtransformation not implemented in this coord; results may be wrong.",
172-
call. = FALSE
173-
)
170+
warn("range backtransformation not implemented in this coord; results may be wrong.")
174171
list(x = panel_params$x_range, y = panel_params$y_range)
175172
},
176173

@@ -436,20 +433,14 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
436433
if (is.character(label_axes)) {
437434
label_axes <- parse_axes_labeling(label_axes)
438435
} else if (!is.list(label_axes)) {
439-
stop(
440-
"Panel labeling format not recognized.",
441-
call. = FALSE
442-
)
436+
abort("Panel labeling format not recognized.")
443437
label_axes <- list(left = "N", bottom = "E")
444438
}
445439

446440
if (is.character(label_graticule)) {
447441
label_graticule <- unlist(strsplit(label_graticule, ""))
448442
} else {
449-
stop(
450-
"Graticule labeling format not recognized.",
451-
call. = FALSE
452-
)
443+
abort("Graticule labeling format not recognized.")
453444
label_graticule <- ""
454445
}
455446

0 commit comments

Comments
 (0)