Skip to content

Commit 9eeeafe

Browse files
authored
Improve consistency of naming internal functions (#6072)
* rename `validate_facets()` to `check_facets()` * write prevalidation/postvalidation as checks * more responsibility for `check_nondata_cols()` * `check_subclass()` is actually validator * path checker is validator * plot_dev is a validator * `check_breaks_labels()` has no return value * make `check_scale_type()` consistent * `validate_theme/element()` is a check * update labeller deprecation message * `check_labeller()` performs an update * `check_linewidth()` performs an update * `check_polar_guide()` is validator * `update_labeller()` -> `fix_labeller()` * `update_linewidth()` -> `fix_linewidth()` * `check_facet_class()` --> `check_vars()`
1 parent 628d7ab commit 9eeeafe

39 files changed

+183
-185
lines changed

R/coord-radial.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
191191
# Validate appropriateness of guides
192192
drop_guides <- character(0)
193193
for (type in aesthetics) {
194-
drop_guides <- check_polar_guide(drop_guides, guides, type)
194+
drop_guides <- validate_polar_guide(drop_guides, guides, type)
195195
}
196196

197197
guide_params <- guides$get_params(aesthetics)
@@ -603,7 +603,7 @@ theta_grid <- function(theta, element, inner_radius = c(0, 0.4),
603603
)
604604
}
605605

606-
check_polar_guide <- function(drop_list, guides, type = "theta") {
606+
validate_polar_guide <- function(drop_list, guides, type = "theta") {
607607
guide <- guides$get_guide(type)
608608
primary <- gsub("\\.sec$", "", type)
609609
if (inherits(guide, "GuideNone") || primary %in% guide$available_aes) {

R/facet-.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ df.grid <- function(a, b) {
443443
# facetting variables.
444444

445445
as_facets_list <- function(x) {
446-
x <- validate_facets(x)
446+
check_vars(x)
447447
if (is_quosures(x)) {
448448
x <- quos_auto_name(x)
449449
return(list(x))
@@ -487,7 +487,7 @@ as_facets_list <- function(x) {
487487
x
488488
}
489489

490-
validate_facets <- function(x) {
490+
check_vars <- function(x) {
491491
if (is.mapping(x)) {
492492
cli::cli_abort("Please use {.fn vars} to supply facet variables.")
493493
}
@@ -499,7 +499,7 @@ validate_facets <- function(x) {
499499
"i" = "Did you use {.code %>%} or {.code |>} instead of {.code +}?"
500500
))
501501
}
502-
x
502+
invisible()
503503
}
504504

505505
# Flatten a list of quosures objects to a quosures object, and compact it

R/facet-grid-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ facet_grid <- function(rows = NULL, cols = NULL, scales = "fixed",
177177
facets_list <- grid_as_facets_list(rows, cols)
178178

179179
# Check for deprecated labellers
180-
labeller <- check_labeller(labeller)
180+
labeller <- fix_labeller(labeller)
181181

182182
ggproto(NULL, FacetGrid,
183183
shrink = shrink,

R/facet-wrap.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
174174
)
175175

176176
# Check for deprecated labellers
177-
labeller <- check_labeller(labeller)
177+
labeller <- fix_labeller(labeller)
178178

179179
# Flatten all facets dimensions into a single one
180180
facets <- compact_facets(facets)

R/fortify.R

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,46 @@ fortify.grouped_df <- function(model, data, ...) {
4444
# There are a lot of ways that dim(), colnames(), or as.data.frame() could
4545
# do non-sensical things (they are not even guaranteed to work!) hence the
4646
# paranoid mode.
47-
.prevalidate_data_frame_like_object <- function(data) {
47+
check_data_frame_like <- function(data) {
4848
orig_dims <- dim(data)
49-
if (!vec_is(orig_dims, integer(), size=2))
50-
cli::cli_abort(paste0("{.code dim(data)} must return ",
51-
"an {.cls integer} of length 2."))
52-
if (anyNA(orig_dims) || any(orig_dims < 0)) # extra-paranoid mode
53-
cli::cli_abort(paste0("{.code dim(data)} can't have {.code NA}s ",
54-
"or negative values."))
49+
if (!vec_is(orig_dims, integer(), size = 2)) {
50+
cli::cli_abort(
51+
"{.code dim(data)} must return an {.cls integer} of length 2."
52+
)
53+
}
54+
if (anyNA(orig_dims) || any(orig_dims < 0)) { # extra-paranoid mode
55+
cli::cli_abort(
56+
"{.code dim(data)} can't have {.code NA}s or negative values."
57+
)
58+
}
5559
orig_colnames <- colnames(data)
56-
if (!vec_is(orig_colnames, character(), size = ncol(data)))
57-
cli::cli_abort(paste0("{.code colnames(data)} must return a ",
58-
"{.cls character} of length {.code ncol(data)}."))
60+
if (!vec_is(orig_colnames, character(), size = ncol(data))) {
61+
cli::cli_abort(
62+
"{.code colnames(data)} must return a {.cls character} of length {.code ncol(data)}."
63+
)
64+
}
65+
invisible()
5966
}
60-
.postvalidate_data_frame_like_object <- function(df, data) {
67+
check_data_frame_conversion <- function(new, old) {
6168
msg0 <- "{.code as.data.frame(data)} must "
62-
if (!is.data.frame(df))
69+
if (!is.data.frame(new)) {
6370
cli::cli_abort(paste0(msg0, "return a {.cls data.frame}."))
64-
if (!identical(dim(df), dim(data)))
71+
}
72+
if (!identical(dim(new), dim(old))) {
6573
cli::cli_abort(paste0(msg0, "preserve dimensions."))
66-
if (!identical(colnames(df), colnames(data)))
74+
}
75+
if (!identical(colnames(new), colnames(old))) {
6776
cli::cli_abort(paste0(msg0, "preserve column names."))
77+
}
78+
invisible()
6879
}
6980
validate_as_data_frame <- function(data) {
70-
if (is.data.frame(data))
81+
if (is.data.frame(data)) {
7182
return(data)
72-
.prevalidate_data_frame_like_object(data)
83+
}
84+
check_data_frame_like(data)
7385
df <- as.data.frame(data)
74-
.postvalidate_data_frame_like_object(df, data)
86+
check_data_frame_conversion(df, data)
7587
df
7688
}
7789

R/geom-.R

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,11 @@ Geom <- ggproto("Geom",
170170
)
171171

172172
# Check that all output are valid data
173-
nondata_modified <- check_nondata_cols(modified_aes)
174-
if (length(nondata_modified) > 0) {
175-
issues <- paste0("{.code ", nondata_modified, " = ", as_label(modifiers[[nondata_modified]]), "}")
176-
names(issues) <- rep("x", length(issues))
177-
cli::cli_abort(c(
178-
"Aesthetic modifiers returned invalid values",
179-
"x" = "The following mappings are invalid",
180-
issues,
181-
"i" = "Did you map the modifier in the wrong layer?"
182-
))
183-
}
173+
check_nondata_cols(
174+
modified_aes, modifiers,
175+
problem = "Aesthetic modifiers returned invalid values.",
176+
hint = "Did you map the modifier in the wrong layer?"
177+
)
184178

185179
modified_aes <- cleanup_mismatched_data(modified_aes, nrow(data), "after_scale")
186180

@@ -283,7 +277,7 @@ check_aesthetics <- function(x, n) {
283277
))
284278
}
285279

286-
check_linewidth <- function(data, name) {
280+
fix_linewidth <- function(data, name) {
287281
if (is.null(data$linewidth) && !is.null(data$size)) {
288282
deprecate_soft0("3.4.0", I(paste0("Using the `size` aesthetic with ", name)), I("the `linewidth` aesthetic"))
289283
data$linewidth <- data$size

R/geom-boxplot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
286286
whisker_gp = NULL, staple_gp = NULL, median_gp = NULL,
287287
box_gp = NULL, notch = FALSE, notchwidth = 0.5,
288288
staplewidth = 0, varwidth = FALSE, flipped_aes = FALSE) {
289-
data <- check_linewidth(data, snake_class(self))
289+
data <- fix_linewidth(data, snake_class(self))
290290
data <- flip_data(data, flipped_aes)
291291
# this may occur when using geom_boxplot(stat = "identity")
292292
if (nrow(data) != 1) {

R/geom-crossbar.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
8484
draw_panel = function(self, data, panel_params, coord, lineend = "butt",
8585
linejoin = "mitre", fatten = 2.5, width = NULL,
8686
flipped_aes = FALSE, middle_gp = NULL, box_gp = NULL) {
87-
88-
data <- check_linewidth(data, snake_class(self))
87+
data <- fix_linewidth(data, snake_class(self))
8988
data <- flip_data(data, flipped_aes)
9089

9190
middle <- transform(data, x = xmin, xend = xmax, yend = y, linewidth = linewidth * fatten, alpha = NA)

R/geom-defaults.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ get_geom_defaults <- function(geom, theme = theme_get()) {
106106
return(data)
107107
}
108108
if (is.character(geom)) {
109-
geom <- check_subclass(geom, "Geom")
109+
geom <- validate_subclass(geom, "Geom")
110110
}
111111
if (is.geom(geom)) {
112112
out <- geom$use_defaults(data = NULL, theme = theme)
@@ -126,7 +126,7 @@ reset_stat_defaults <- function() reset_defaults("stat")
126126
cache_defaults <- new_environment()
127127

128128
update_defaults <- function(name, subclass, new, env = parent.frame()) {
129-
obj <- check_subclass(name, subclass, env = env)
129+
obj <- validate_subclass(name, subclass, env = env)
130130
index <- snake_class(obj)
131131

132132
if (is.null(new)) { # Reset from cache

R/geom-errorbar.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ GeomErrorbar <- ggproto("GeomErrorbar", Geom,
9393
# Note: `width` is vestigial
9494
draw_panel = function(self, data, panel_params, coord, lineend = "butt",
9595
width = NULL, flipped_aes = FALSE) {
96-
data <- check_linewidth(data, snake_class(self))
96+
data <- fix_linewidth(data, snake_class(self))
9797
data <- flip_data(data, flipped_aes)
9898
x <- vec_interleave(data$xmin, data$xmax, NA, data$x, data$x, NA, data$xmin, data$xmax)
9999
y <- vec_interleave(data$ymax, data$ymax, NA, data$ymax, data$ymin, NA, data$ymin, data$ymin)

0 commit comments

Comments
 (0)