Skip to content

Commit e0d54f6

Browse files
authored
Update errors, warnings, and messages to cli (#4796)
1 parent eb0010e commit e0d54f6

File tree

237 files changed

+2936
-939
lines changed

Some content is hidden

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

237 files changed

+2936
-939
lines changed

R/aes-evaluation.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ is_calculated <- function(x) {
122122
} else if (is.pairlist(x)) {
123123
FALSE
124124
} else {
125-
abort(glue("Unknown input: {class(x)[1]}"))
125+
cli::cli_abort("Unknown input: {.cls {class(x)[1]}}")
126126
}
127127
}
128128
is_scaled <- function(x) {
@@ -170,7 +170,7 @@ strip_dots <- function(expr, env, strip_pronoun = FALSE) {
170170
# For list of aesthetics
171171
lapply(expr, strip_dots, env = env, strip_pronoun = strip_pronoun)
172172
} else {
173-
abort(glue("Unknown input: {class(expr)[1]}"))
173+
cli::cli_abort("Unknown input: {.cls {class(expr)[1]}}")
174174
}
175175
}
176176

R/aes.r

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ new_aesthetic <- function(x, env = globalenv()) {
113113
}
114114
new_aes <- function(x, env = globalenv()) {
115115
if (!is.list(x)) {
116-
abort("`x` must be a list")
116+
cli::cli_abort("{.arg x} must be a list")
117117
}
118118
x <- lapply(x, new_aesthetic, env = env)
119119
structure(x, class = "uneval")
@@ -179,8 +179,7 @@ rename_aes <- function(x) {
179179
names(x) <- standardise_aes_names(names(x))
180180
duplicated_names <- names(x)[duplicated(names(x))]
181181
if (length(duplicated_names) > 0L) {
182-
duplicated_message <- paste0(unique(duplicated_names), collapse = ", ")
183-
warn(glue("Duplicated aesthetics after name standardisation: {duplicated_message}"))
182+
cli::cli_warn("Duplicated aesthetics after name standardisation: {.field {unique(duplicated_names)}}")
184183
}
185184
x
186185
}
@@ -283,7 +282,7 @@ aes_ <- function(x, y, ...) {
283282
} else if (is.call(x) || is.name(x) || is.atomic(x)) {
284283
new_aesthetic(x, caller_env)
285284
} else {
286-
abort("Aesthetic must be a one-sided formula, call, name, or constant.")
285+
cli::cli_abort("Aesthetic must be a one-sided formula, call, name, or constant.")
287286
}
288287
}
289288
mapping <- lapply(mapping, as_quosure_aes)
@@ -351,7 +350,7 @@ aes_auto <- function(data = NULL, ...) {
351350

352351
# detect names of data
353352
if (is.null(data)) {
354-
abort("aes_auto requires data.frame or names of data.frame.")
353+
cli::cli_abort("{.fn aes_auto} requires a {.cls data.frame} or names of data.frame.")
355354
} else if (is.data.frame(data)) {
356355
vars <- names(data)
357356
} else {
@@ -400,7 +399,10 @@ warn_for_aes_extract_usage_expr <- function(x, data, env = emptyenv()) {
400399
if (is_call(x, "[[") || is_call(x, "$")) {
401400
if (extract_target_is_likely_data(x, data, env)) {
402401
good_usage <- alternative_aes_extract_usage(x)
403-
warn(glue("Use of `{format(x)}` is discouraged. Use `{good_usage}` instead."))
402+
cli::cli_warn(c(
403+
"Use of {.code {format(x)}} is discouraged.",
404+
"i" = "Use {.code {good_usage}} instead."
405+
))
404406
}
405407
} else if (is.call(x)) {
406408
lapply(x, warn_for_aes_extract_usage_expr, data, env)
@@ -414,7 +416,7 @@ alternative_aes_extract_usage <- function(x) {
414416
} else if (is_call(x, "$")) {
415417
as.character(x[[3]])
416418
} else {
417-
abort(glue("Don't know how to get alternative usage for `{format(x)}`"))
419+
cli::cli_abort("Don't know how to get alternative usage for {.var {x}}")
418420
}
419421
}
420422

R/annotation-custom.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +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-
abort("annotation_custom only works with Cartesian coordinates")
74+
cli::cli_abort("{.fn annotation_custom} only works with {.fn coord_cartesian}")
7575
}
7676
corners <- new_data_frame(list(x = c(xmin, xmax), y = c(ymin, ymax)), n = 2)
7777
data <- coord$transform(corners, panel_params)

R/annotation-map.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ NULL
5959
annotation_map <- function(map, ...) {
6060
# Get map input into correct form
6161
if (!is.data.frame(map)) {
62-
abort("`map` must be a data.frame")
62+
cli::cli_abort("{.arg map} must be a {.cls data.frame}")
6363
}
6464
if (!is.null(map$lat)) map$y <- map$lat
6565
if (!is.null(map$long)) map$x <- map$long
6666
if (!is.null(map$region)) map$id <- map$region
6767
if (!all(c("x", "y", "id") %in% names(map))) {
68-
abort("`map`must have the columns `x`, `y`, and `id`")
68+
cli::cli_abort("{.arg map} must have the columns {.col x}, {.col y}, and {.col id}")
6969
}
7070

7171
layer(

R/annotation-raster.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ GeomRasterAnn <- ggproto("GeomRasterAnn", Geom,
7474
draw_panel = function(data, panel_params, coord, raster, xmin, xmax,
7575
ymin, ymax, interpolate = FALSE) {
7676
if (!inherits(coord, "CoordCartesian")) {
77-
abort("annotation_raster only works with Cartesian coordinates")
77+
cli::cli_abort("{.fn annotation_raster} only works with {.fn coord_cartesian}")
7878
}
7979
corners <- new_data_frame(list(x = c(xmin, xmax), y = c(ymin, ymax)), n = 2)
8080
data <- coord$transform(corners, panel_params)

R/annotation.r

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
4343
na.rm = FALSE) {
4444

4545
if (geom %in% c("abline", "hline", "vline")) {
46-
warn(c(
47-
glue("`annotate()` does not support `geom = \"{geom}\"`."),
48-
i = glue("Please use `geom_{geom}()` directly instead.")
46+
cli::cli_warn(c(
47+
"{.arg geom} must not be {.val {geom}}.",
48+
"i" = "Please use {.fn {paste0('geom_', geom)}} directly instead."
4949
))
5050
}
5151

@@ -67,9 +67,8 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
6767
# if there is still more than one unique length, we error out
6868
if (length(n) > 1L) {
6969
bad <- lengths != 1L
70-
details <- paste(names(aesthetics)[bad], " (", lengths[bad], ")",
71-
sep = "", collapse = ", ")
72-
abort(glue("Unequal parameter lengths: {details}"))
70+
details <- paste0(names(aesthetics)[bad], " (", lengths[bad], ")")
71+
cli::cli_abort("Unequal parameter lengths: {details}")
7372
}
7473

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

R/autolayer.r

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,5 @@ autolayer <- function(object, ...) {
1515

1616
#' @export
1717
autolayer.default <- function(object, ...) {
18-
abort(glue(
19-
"Objects of type ",
20-
glue_collapse(class(object), "/"),
21-
" not supported by autolayer."
22-
))
18+
cli::cli_abort("No autolayer method avialable for {.cls {class(object)[1]}} objects")
2319
}

R/autoplot.r

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ autoplot <- function(object, ...) {
1515

1616
#' @export
1717
autoplot.default <- function(object, ...) {
18-
abort(glue(
19-
"Objects of type ",
20-
glue_collapse(class(object), "/"),
21-
" not supported by autoplot."
18+
cli::cli_abort(c(
19+
"Objects of class {.cls {class(object)[[1]]}} are not supported by autoplot.",
20+
"i" = "have you loaded the required package?"
2221
))
2322
}
2423

R/axis-secondary.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ is.sec_axis <- function(x) {
122122
set_sec_axis <- function(sec.axis, scale) {
123123
if (!is.waive(sec.axis)) {
124124
if (is.formula(sec.axis)) sec.axis <- sec_axis(sec.axis)
125-
if (!is.sec_axis(sec.axis)) abort("Secondary axes must be specified using 'sec_axis()'")
125+
if (!is.sec_axis(sec.axis)) {
126+
cli::cli_abort("Secondary axes must be specified using {.fn sec_axis}")
127+
}
126128
scale$secondary.axis <- sec.axis
127129
}
128130
return(scale)
@@ -159,8 +161,12 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
159161

160162
# Inherit settings from the primary axis/scale
161163
init = function(self, scale) {
162-
if (self$empty()) return()
163-
if (!is.function(self$trans)) abort("transformation for secondary axes must be a function")
164+
if (self$empty()) {
165+
return()
166+
}
167+
if (!is.function(self$trans)) {
168+
cli::cli_abort("Transformation for secondary axes must be a function")
169+
}
164170
if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name
165171
if (is.derived(self$breaks)) self$breaks <- scale$breaks
166172
if (is.waive(self$breaks)) self$breaks <- scale$trans$breaks
@@ -188,7 +194,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
188194

189195
# Test for monotonicity
190196
if (length(unique(sign(diff(full_range)))) != 1)
191-
abort("transformation for secondary axes must be monotonic")
197+
cli::cli_abort("Transformation for secondary axes must be monotonic")
192198
},
193199

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

R/bench.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ benchplot <- function(x) {
1616
x <- enquo(x)
1717
construct <- system.time(x <- eval_tidy(x))
1818
if (!inherits(x, "ggplot")) {
19-
abort("`x` must be a ggplot object")
19+
cli::cli_abort("{.arg x} must be a {.cls ggplot} object")
2020
}
2121

2222
build <- system.time(data <- ggplot_build(x))

R/bin.R

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
bins <- function(breaks, closed = "right",
22
fuzz = 1e-08 * stats::median(diff(breaks))) {
3-
if (!is.numeric(breaks)) abort("`breaks` must be a numeric vector")
3+
if (!is.numeric(breaks)) {
4+
cli::cli_abort("{.arg breaks} must be a numeric vector")
5+
}
46
closed <- arg_match0(closed, c("right", "left"))
57

68
breaks <- sort(breaks)
@@ -50,18 +52,22 @@ bin_breaks <- function(breaks, closed = c("right", "left")) {
5052

5153
bin_breaks_width <- function(x_range, width = NULL, center = NULL,
5254
boundary = NULL, closed = c("right", "left")) {
53-
if (length(x_range) != 2) abort("`x_range` must have two elements")
55+
if (length(x_range) != 2) {
56+
cli::cli_abort("{.arg x_range} must have two elements")
57+
}
5458

5559
# if (length(x_range) == 0) {
5660
# return(bin_params(numeric()))
5761
# }
58-
if (!(is.numeric(width) && length(width) == 1)) abort("`width` must be a numeric scalar")
62+
if (!(is.numeric(width) && length(width) == 1)) {
63+
cli::cli_abort("{.arg width} must be a number")
64+
}
5965
if (width <= 0) {
60-
abort("`binwidth` must be positive")
66+
cli::cli_abort("{.arg binwidth} must be positive")
6167
}
6268

6369
if (!is.null(boundary) && !is.null(center)) {
64-
abort("Only one of 'boundary' and 'center' may be specified.")
70+
cli::cli_abort("Only one of {.arg boundary} and {.arg center} may be specified.")
6571
} else if (is.null(boundary)) {
6672
if (is.null(center)) {
6773
# If neither edge nor center given, compute both using tile layer's
@@ -87,7 +93,10 @@ bin_breaks_width <- function(x_range, width = NULL, center = NULL,
8793
max_x <- x_range[2] + (1 - 1e-08) * width
8894

8995
if (isTRUE((max_x - origin) / width > 1e6)) {
90-
abort("The number of histogram bins must be less than 1,000,000.\nDid you make `binwidth` too small?")
96+
cli::cli_abort(c(
97+
"The number of histogram bins must be less than 1,000,000.",
98+
"i" = "Did you make {.arg binwidth} too small?"
99+
))
91100
}
92101
breaks <- seq(origin, max_x, width)
93102

@@ -102,11 +111,13 @@ bin_breaks_width <- function(x_range, width = NULL, center = NULL,
102111

103112
bin_breaks_bins <- function(x_range, bins = 30, center = NULL,
104113
boundary = NULL, closed = c("right", "left")) {
105-
if (length(x_range) != 2) abort("`x_range` must have two elements")
114+
if (length(x_range) != 2) {
115+
cli::cli_abort("{.arg x_range} must have two elements")
116+
}
106117

107118
bins <- as.integer(bins)
108119
if (bins < 1) {
109-
abort("Need at least one bin.")
120+
cli::cli_abort("{.arg bins} must be 1 or greater")
110121
} else if (zero_range(x_range)) {
111122
# 0.1 is the same width as the expansion `default_expansion()` gives for 0-width data
112123
width <- 0.1
@@ -125,7 +136,9 @@ bin_breaks_bins <- function(x_range, bins = 30, center = NULL,
125136
# Compute bins ------------------------------------------------------------
126137

127138
bin_vector <- function(x, bins, weight = NULL, pad = FALSE) {
128-
if (!is_bins(bins)) abort("`bins` must be a ggplot2_bins object")
139+
if (!is_bins(bins)) {
140+
cli::cli_abort("{.arg bins} must be a {.cls ggplot2_bins} object")
141+
}
129142

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

R/compat-plyr.R

Lines changed: 6 additions & 4 deletions
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-
abort("Can only remove rownames from data.frame and matrix objects")
22+
cli::cli_abort("Can only remove rownames from {.cls data.frame} and {.cls 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-
abort("x is not a factor or character vector")
196+
cli::cli_abort("{.arg x} must be a factor or character vector")
197197
}
198198
x
199199
}
@@ -239,14 +239,16 @@ as.quoted <- function(x, env = parent.frame()) {
239239
} else if (is.call(x)) {
240240
as.list(x)[-1]
241241
} else {
242-
abort("Only knows how to quote characters, calls, and formula")
242+
cli::cli_abort("Must be a character vector, call, or 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)) abort("`x` must be numeric")
249+
if (!is.numeric(x)) {
250+
cli::cli_abort("{.arg x} must be numeric")
251+
}
250252
f(x/accuracy) * accuracy
251253
}
252254
#' Bind data frames together by common column names

R/coord-.r

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,28 @@ Coord <- ggproto("Coord",
6363

6464
render_fg = function(panel_params, theme) element_render(theme, "panel.border"),
6565

66-
render_bg = function(panel_params, theme) {
67-
abort("Not implemented")
66+
render_bg = function(self, panel_params, theme) {
67+
cli::cli_abort("{.fn {snake_class(self)}} has not implemented a {.fn render_bg} method")
6868
},
6969

70-
render_axis_h = function(panel_params, theme) {
71-
abort("Not implemented")
70+
render_axis_h = function(self, panel_params, theme) {
71+
cli::cli_abort("{.fn {snake_class(self)}} has not implemented a {.fn render_axis_h} method")
7272
},
7373

74-
render_axis_v = function(panel_params, theme) {
75-
abort("Not implemented")
74+
render_axis_v = function(self, panel_params, theme) {
75+
cli::cli_abort("{.fn {snake_class(self)}} has not implemented a {.fn render_axis_v} method")
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-
abort("Not implemented")
82+
cli::cli_abort("{.fn {snake_class(self)}} has not implemented a {.fn backtransform_range} method")
8383
},
8484

8585
# return range stored in panel_params
86-
range = function(panel_params) {
87-
abort("Not implemented")
86+
range = function(self, panel_params) {
87+
cli::cli_abort("{.fn {snake_class(self)}} has not implemented a {.fn range} method")
8888
},
8989

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

0 commit comments

Comments
 (0)