Skip to content

Move error signalling to rlang::abort() #3526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/aes-calculated.r
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ is_calculated <- function(x) {
} else if (is.pairlist(x)) {
FALSE
} else {
stop("Unknown input:", class(x)[1])
abort(paste0("Unknown input:", class(x)[1]))
}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ strip_dots <- function(expr) {
# For list of aesthetics
lapply(expr, strip_dots)
} else {
stop("Unknown input:", class(expr)[1])
abort(paste0("Unknown input:", class(expr)[1]))
}
}

Expand Down
9 changes: 4 additions & 5 deletions R/aes.r
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ new_aesthetic <- function(x, env = globalenv()) {
x
}
new_aes <- function(x, env = globalenv()) {
stopifnot(is.list(x))
if (!is.list(x)) abort("`x` must be a list")
x <- lapply(x, new_aesthetic, env = env)
structure(x, class = "uneval")
}
Expand Down Expand Up @@ -248,8 +248,7 @@ aes_ <- function(x, y, ...) {
} else if (is.call(x) || is.name(x) || is.atomic(x)) {
new_aesthetic(x, caller_env)
} else {
stop("Aesthetic must be a one-sided formula, call, name, or constant.",
call. = FALSE)
abort("Aesthetic must be a one-sided formula, call, name, or constant.")
}
}
mapping <- lapply(mapping, as_quosure_aes)
Expand Down Expand Up @@ -309,7 +308,7 @@ aes_auto <- function(data = NULL, ...) {

# detect names of data
if (is.null(data)) {
stop("aes_auto requires data.frame or names of data.frame.")
abort("aes_auto requires data.frame or names of data.frame.")
} else if (is.data.frame(data)) {
vars <- names(data)
} else {
Expand Down Expand Up @@ -376,7 +375,7 @@ alternative_aes_extract_usage <- function(x) {
} else if (is_call(x, "$")) {
as.character(x[[3]])
} else {
stop("Don't know how to get alternative usage for `", format(x), "`", call. = FALSE)
abort(paste0("Don't know how to get alternative usage for `", format(x), "`"))
}
}

Expand Down
3 changes: 1 addition & 2 deletions R/annotation-custom.r
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ GeomCustomAnn <- ggproto("GeomCustomAnn", Geom,
draw_panel = function(data, panel_params, coord, grob, xmin, xmax,
ymin, ymax) {
if (!inherits(coord, "CoordCartesian")) {
stop("annotation_custom only works with Cartesian coordinates",
call. = FALSE)
abort("annotation_custom only works with Cartesian coordinates")
}
corners <- new_data_frame(list(x = c(xmin, xmax), y = c(ymin, ymax)), n = 2)
data <- coord$transform(corners, panel_params)
Expand Down
4 changes: 2 additions & 2 deletions R/annotation-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ NULL
#' }
annotation_map <- function(map, ...) {
# Get map input into correct form
stopifnot(is.data.frame(map))
if (!is.data.frame(map)) abort("`map` must be a data.frame")
if (!is.null(map$lat)) map$y <- map$lat
if (!is.null(map$long)) map$x <- map$long
if (!is.null(map$region)) map$id <- map$region
stopifnot(all(c("x", "y", "id") %in% names(map)))
if (!all(c("x", "y", "id") %in% names(map))) abort("`map`must have the columns `x`, `y`, and `id`")

layer(
data = dummy_data(),
Expand Down
3 changes: 1 addition & 2 deletions R/annotation-raster.r
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ GeomRasterAnn <- ggproto("GeomRasterAnn", Geom,
draw_panel = function(data, panel_params, coord, raster, xmin, xmax,
ymin, ymax, interpolate = FALSE) {
if (!inherits(coord, "CoordCartesian")) {
stop("annotation_raster only works with Cartesian coordinates",
call. = FALSE)
abort("annotation_raster only works with Cartesian coordinates")
}
corners <- new_data_frame(list(x = c(xmin, xmax), y = c(ymin, ymax)), n = 2)
data <- coord$transform(corners, panel_params)
Expand Down
2 changes: 1 addition & 1 deletion R/annotation.r
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
bad <- lengths != 1L
details <- paste(names(aesthetics)[bad], " (", lengths[bad], ")",
sep = "", collapse = ", ")
stop("Unequal parameter lengths: ", details, call. = FALSE)
abort(paste0("Unequal parameter lengths: ", details))
}

data <- new_data_frame(position, n = n)
Expand Down
4 changes: 2 additions & 2 deletions R/autolayer.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ autolayer <- function(object, ...) {

#' @export
autolayer.default <- function(object, ...) {
stop("Objects of type ", paste(class(object), collapse = "/"),
" not supported by autolayer.", call. = FALSE)
abort(paste0("Objects of type ", paste(class(object), collapse = "/"),
" not supported by autolayer."))
}
4 changes: 2 additions & 2 deletions R/autoplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ autoplot <- function(object, ...) {

#' @export
autoplot.default <- function(object, ...) {
stop("Objects of type ", paste(class(object), collapse = "/"),
" not supported by autoplot.", call. = FALSE)
abort(paste0("Objects of type ", paste(class(object), collapse = "/"),
" not supported by autoplot."))
}

6 changes: 3 additions & 3 deletions R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ is.sec_axis <- function(x) {
set_sec_axis <- function(sec.axis, scale) {
if (!is.waive(sec.axis)) {
if (is.formula(sec.axis)) sec.axis <- sec_axis(sec.axis)
if (!is.sec_axis(sec.axis)) stop("Secondary axes must be specified using 'sec_axis()'")
if (!is.sec_axis(sec.axis)) abort("Secondary axes must be specified using 'sec_axis()'")
scale$secondary.axis <- sec.axis
}
return(scale)
Expand Down Expand Up @@ -143,7 +143,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
# Inherit settings from the primary axis/scale
init = function(self, scale) {
if (self$empty()) return()
if (!is.function(self$trans)) stop("transformation for secondary axes must be a function", call. = FALSE)
if (!is.function(self$trans)) abort("transformation for secondary axes must be a function")
if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name
if (is.derived(self$breaks)) self$breaks <- scale$breaks
if (is.waive(self$breaks)) self$breaks <- scale$trans$breaks
Expand All @@ -164,7 +164,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,

# Test for monotonicity
if (length(unique(sign(diff(full_range)))) != 1)
stop("transformation for secondary axes must be monotonic")
abort("transformation for secondary axes must be monotonic")
},

break_info = function(self, range, scale) {
Expand Down
2 changes: 1 addition & 1 deletion R/bench.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
benchplot <- function(x) {
x <- enquo(x)
construct <- system.time(x <- eval_tidy(x))
stopifnot(inherits(x, "ggplot"))
if (!inherits(x, "ggplot")) abort("`x` must be a ggplot object")

build <- system.time(data <- ggplot_build(x))
render <- system.time(grob <- ggplot_gtable(data))
Expand Down
18 changes: 9 additions & 9 deletions R/bin.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
bins <- function(breaks, closed = c("right", "left"),
fuzz = 1e-08 * stats::median(diff(breaks))) {
stopifnot(is.numeric(breaks))
if (!is.numeric(breaks)) abort("`breaks` must be a numeric vector")
closed <- match.arg(closed)

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

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

# if (length(x_range) == 0) {
# return(bin_params(numeric()))
# }
stopifnot(is.numeric(width), length(width) == 1)
if (!(is.numeric(width) && length(width) == 1)) abort("`width` must be a numeric scalar")
if (width <= 0) {
stop("`binwidth` must be positive", call. = FALSE)
abort("`binwidth` must be positive")
}

if (!is.null(boundary) && !is.null(center)) {
stop("Only one of 'boundary' and 'center' may be specified.")
abort("Only one of 'boundary' and 'center' may be specified.")
} else if (is.null(boundary)) {
if (is.null(center)) {
# If neither edge nor center given, compute both using tile layer's
Expand All @@ -88,19 +88,19 @@ bin_breaks_width <- function(x_range, width = NULL, center = NULL,
breaks <- seq(origin, max_x, width)

if (length(breaks) > 1e6) {
stop("The number of histogram bins must be less than 1,000,000.\nDid you make `binwidth` too small?", call. = FALSE)
abort("The number of histogram bins must be less than 1,000,000.\nDid you make `binwidth` too small?")
}

bin_breaks(breaks, closed = closed)
}

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

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

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

if (all(is.na(x))) {
return(bin_out(length(x), NA, NA, xmin = NA, xmax = NA))
Expand Down
8 changes: 4 additions & 4 deletions R/compat-plyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ unrowname <- function(x) {
} else if (is.matrix(x)) {
dimnames(x)[1] <- list(NULL)
} else {
stop("Can only remove rownames from data.frame and matrix objects", call. = FALSE)
abort("Can only remove rownames from data.frame and matrix objects")
}
x
}
Expand Down Expand Up @@ -193,7 +193,7 @@ revalue <- function(x, replace) {
lev[match(names(replace), lev)] <- replace
levels(x) <- lev
} else if (!is.null(x)) {
stop("x is not a factor or character vector", call. = FALSE)
abort("x is not a factor or character vector")
}
x
}
Expand Down Expand Up @@ -239,14 +239,14 @@ as.quoted <- function(x, env = parent.frame()) {
} else if (is.call(x)) {
as.list(x)[-1]
} else {
stop("Only knows how to quote characters, calls, and formula", call. = FALSE)
abort("Only knows how to quote characters, calls, and formula")
}
attributes(x) <- list(env = env, class = 'quoted')
x
}
# round a number to a given precision
round_any <- function(x, accuracy, f = round) {
if (!is.numeric(x)) stop("x must be numeric", call. = FALSE)
if (!is.numeric(x)) abort("`x`` must be numeric")
f(x/accuracy) * accuracy
}
#' Bind data frames together by common column names
Expand Down
10 changes: 5 additions & 5 deletions R/coord-.r
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,27 @@ Coord <- ggproto("Coord",
render_fg = function(panel_params, theme) element_render(theme, "panel.border"),

render_bg = function(panel_params, theme) {
stop("Not implemented", call. = FALSE)
abort("Not implemented")
},

render_axis_h = function(panel_params, theme) {
stop("Not implemented", call. = FALSE)
abort("Not implemented")
},

render_axis_v = function(panel_params, theme) {
stop("Not implemented", call. = FALSE)
abort("Not implemented")
},

# transform range given in transformed coordinates
# back into range in given in (possibly scale-transformed)
# data coordinates
backtransform_range = function(self, panel_params) {
stop("Not implemented", call. = FALSE)
abort("Not implemented")
},

# return range stored in panel_params
range = function(panel_params) {
stop("Not implemented", call. = FALSE)
abort("Not implemented")
},

setup_panel_params = function(scale_x, scale_y, params = list()) {
Expand Down
14 changes: 4 additions & 10 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
}

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

Expand All @@ -109,7 +109,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
}

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

Expand Down Expand Up @@ -434,20 +434,14 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
if (is.character(label_axes)) {
label_axes <- parse_axes_labeling(label_axes)
} else if (!is.list(label_axes)) {
stop(
"Panel labeling format not recognized.",
call. = FALSE
)
abort("Panel labeling format not recognized.")
label_axes <- list(left = "N", bottom = "E")
}

if (is.character(label_graticule)) {
label_graticule <- unlist(strsplit(label_graticule, ""))
} else {
stop(
"Graticule labeling format not recognized.",
call. = FALSE
)
abort("Graticule labeling format not recognized.")
label_graticule <- ""
}

Expand Down
23 changes: 9 additions & 14 deletions R/facet-.r
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ Facet <- ggproto("Facet", NULL,
params = list(),

compute_layout = function(data, params) {
stop("Not implemented", call. = FALSE)
abort("Not implemented")
},
map_data = function(data, layout, params) {
stop("Not implemented", call. = FALSE)
abort("Not implemented")
},
init_scales = function(layout, x_scale = NULL, y_scale = NULL, params) {
scales <- list()
Expand Down Expand Up @@ -125,7 +125,7 @@ Facet <- ggproto("Facet", NULL,
rep(list(zeroGrob()), length(unique(layout$PANEL)))
},
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
stop("Not implemented", call. = FALSE)
abort("Not implemented")
},
draw_labels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, labels, params) {
panel_dim <- find_panel(panels)
Expand Down Expand Up @@ -276,7 +276,7 @@ df.grid <- function(a, b) {

as_facets_list <- function(x) {
if (inherits(x, "uneval")) {
stop("Please use `vars()` to supply facet variables", call. = FALSE)
abort("Please use `vars()` to supply facet variables")
}
if (is_quosures(x)) {
x <- quos_auto_name(x)
Expand Down Expand Up @@ -446,11 +446,7 @@ check_layout <- function(x) {
return()
}

stop(
"Facet layout has bad format. ",
"It must contain columns 'PANEL', 'SCALE_X', and 'SCALE_Y'",
call. = FALSE
)
abort("Facet layout has bad format. It must contain columns 'PANEL', 'SCALE_X', and 'SCALE_Y'")
}


Expand Down Expand Up @@ -541,12 +537,11 @@ combine_vars <- function(data, env = emptyenv(), vars = NULL, drop = TRUE) {
missing_txt <- vapply(missing, var_list, character(1))
name <- c("Plot", paste0("Layer ", seq_len(length(data) - 1)))

stop(
abort(paste0(
"At least one layer must contain all faceting variables: ",
var_list(names(vars)), ".\n",
paste0("* ", name, " is missing ", missing_txt, collapse = "\n"),
call. = FALSE
)
paste0("* ", name, " is missing ", missing_txt, collapse = "\n")
))
}

base <- unique(rbind_dfs(values[has_all]))
Expand All @@ -567,7 +562,7 @@ combine_vars <- function(data, env = emptyenv(), vars = NULL, drop = TRUE) {
}

if (empty(base)) {
stop("Faceting variables must have at least one value", call. = FALSE)
abort("Faceting variables must have at least one value")
}

base
Expand Down
Loading