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)

R/geom-hex.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ geom_hex <- function(mapping = NULL, data = NULL,
5858
GeomHex <- ggproto("GeomHex", Geom,
5959
draw_group = function(self, data, panel_params, coord, lineend = "butt",
6060
linejoin = "mitre", linemitre = 10) {
61-
data <- check_linewidth(data, snake_class(self))
61+
data <- fix_linewidth(data, snake_class(self))
6262
if (empty(data)) {
6363
return(zeroGrob())
6464
}

R/geom-path.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ GeomPath <- ggproto("GeomPath", Geom,
165165
draw_panel = function(self, data, panel_params, coord, arrow = NULL, arrow.fill = NULL,
166166
lineend = "butt", linejoin = "round", linemitre = 10,
167167
na.rm = FALSE) {
168-
data <- check_linewidth(data, snake_class(self))
168+
data <- fix_linewidth(data, snake_class(self))
169169
if (!anyDuplicated(data$group)) {
170170
cli::cli_inform(c(
171171
"{.fn {snake_class(self)}}: Each group consists of only one observation.",

R/geom-polygon.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ geom_polygon <- function(mapping = NULL, data = NULL,
109109
GeomPolygon <- ggproto("GeomPolygon", Geom,
110110
draw_panel = function(self, data, panel_params, coord, rule = "evenodd",
111111
lineend = "butt", linejoin = "round", linemitre = 10) {
112-
data <- check_linewidth(data, snake_class(self))
112+
data <- fix_linewidth(data, snake_class(self))
113113
n <- nrow(data)
114114
if (n == 1) return(zeroGrob())
115115

R/geom-rect.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ GeomRect <- ggproto("GeomRect", Geom,
6969
},
7070

7171
draw_panel = function(self, data, panel_params, coord, lineend = "butt", linejoin = "mitre") {
72-
data <- check_linewidth(data, snake_class(self))
72+
data <- fix_linewidth(data, snake_class(self))
7373
if (!coord$is_linear()) {
7474
aesthetics <- setdiff(
7575
names(data), c("x", "y", "xmin", "xmax", "ymin", "ymax")

R/geom-ribbon.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
157157
draw_group = function(self, data, panel_params, coord, lineend = "butt",
158158
linejoin = "round", linemitre = 10, na.rm = FALSE,
159159
flipped_aes = FALSE, outline.type = "both") {
160-
data <- check_linewidth(data, snake_class(self))
160+
data <- fix_linewidth(data, snake_class(self))
161161
data <- flip_data(data, flipped_aes)
162162
data <- data[order(data$group), ]
163163

R/geom-rug.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ GeomRug <- ggproto("GeomRug", Geom,
9090

9191
draw_panel = function(self, data, panel_params, coord, lineend = "butt",
9292
sides = "bl", outside = FALSE, length = unit(0.03, "npc")) {
93-
data <- check_linewidth(data, snake_class(self))
93+
data <- fix_linewidth(data, snake_class(self))
9494
check_inherits(length, "unit")
9595
rugs <- list()
9696
data <- coord$transform(data, panel_params)

R/geom-segment.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ GeomSegment <- ggproto("GeomSegment", Geom,
116116
lineend = "butt", linejoin = "round", na.rm = FALSE) {
117117
data$xend <- data$xend %||% data$x
118118
data$yend <- data$yend %||% data$y
119-
data <- check_linewidth(data, snake_class(self))
119+
data <- fix_linewidth(data, snake_class(self))
120120
data <- remove_missing(data, na.rm = na.rm,
121121
c("x", "y", "xend", "yend", "linetype", "linewidth"),
122122
name = "geom_segment"

R/guide-.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ NULL
1919
new_guide <- function(..., available_aes = "any", super) {
2020

2121
pf <- parent.frame()
22-
super <- check_subclass(super, "Guide", env = pf)
22+
super <- validate_subclass(super, "Guide", env = pf)
2323

2424
args <- list2(...)
2525

@@ -51,7 +51,7 @@ new_guide <- function(..., available_aes = "any", super) {
5151
# Validate theme settings
5252
if (!is.null(params$theme)) {
5353
check_object(params$theme, is.theme, what = "a {.cls theme} object")
54-
validate_theme(params$theme, call = caller_env())
54+
check_theme(params$theme, call = caller_env())
5555
params$direction <- params$direction %||% params$theme$legend.direction
5656
}
5757

R/labeller.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -577,21 +577,21 @@ assemble_strips <- function(grobs, theme, horizontal = TRUE, clip) {
577577
})
578578
}
579579

580-
# Check for old school labeller
581-
check_labeller <- function(labeller) {
580+
# Repair old school labeller
581+
fix_labeller <- function(labeller) {
582582
labeller <- match.fun(labeller)
583583
is_deprecated <- all(c("variable", "value") %in% names(formals(labeller)))
584584

585585
if (is_deprecated) {
586+
deprecate_warn0(
587+
"2.0.0", what = "facet_(labeller)",
588+
details =
589+
"Modern labellers do not take `variable` and `value` arguments anymore."
590+
)
586591
old_labeller <- labeller
587592
labeller <- function(labels) {
588593
Map(old_labeller, names(labels), labels)
589594
}
590-
# TODO Update to lifecycle after next lifecycle release
591-
cli::cli_warn(c(
592-
"The {.arg labeller} API has been updated. Labellers taking {.arg variable} and {.arg value} arguments are now deprecated.",
593-
"i" = "See labellers documentation."
594-
))
595595
}
596596

597597
labeller

0 commit comments

Comments
 (0)