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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 9 additions & 14 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 5 additions & 2 deletions
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

0 commit comments

Comments
 (0)