Skip to content
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

Clean up orphan functions and tiny one-off helpers #6022

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
20fa50f
replace single use internal functions
teunbrand Aug 1, 2024
0bfbcc3
move helper to location where it is used
teunbrand Aug 1, 2024
79866e8
remove orphan functions
teunbrand Aug 1, 2024
a5a1ce5
replace and remove redundant `adjust_breaks()` function
teunbrand Aug 1, 2024
c4c61de
replace `df_rows()` with `vec_slice()`
teunbrand Aug 1, 2024
2ce92fb
inline `f_as_facets_list()`
teunbrand Aug 1, 2024
15241a6
inline `find_origin()`
teunbrand Aug 1, 2024
2e9b8c2
inline `firstUpper()`
teunbrand Aug 1, 2024
0cca7a3
replace `has_name()`
teunbrand Aug 1, 2024
5f4a4b9
replace `interleave()` with `vec_interleave()`
teunbrand Aug 1, 2024
79fa0b1
remove `cunion()`
teunbrand Aug 1, 2024
abc62dd
inline `is_dotted_var()`
teunbrand Aug 1, 2024
4a3ffff
inline `is_facets()`
teunbrand Aug 1, 2024
9976913
inline `is_labeller()`
teunbrand Aug 1, 2024
b4e0d80
inline `is_missing_arg()`
teunbrand Aug 1, 2024
be4081a
replace `is_npc()` (we partially backport `unitType()`)
teunbrand Aug 1, 2024
70cc78b
inline `is_scalar_numeric()`
teunbrand Aug 1, 2024
997d9fe
inline `is.margin()`
teunbrand Aug 1, 2024
1bdc20d
inline `is.sec_axis()`
teunbrand Aug 1, 2024
b044fa2
inline `is.subclass()`
teunbrand Aug 1, 2024
490c9c3
inline `is_triple_bang()`
teunbrand Aug 1, 2024
4ebdea3
remove `justify_grobs()`
teunbrand Aug 2, 2024
e086817
inline `label_variable()`
teunbrand Aug 2, 2024
024a1b6
more responsibility for `parse_axes_labeling()`, so that it is less d…
teunbrand Aug 2, 2024
b2de86e
remove `resolve_guide()`
teunbrand Aug 2, 2024
f7c8cb6
inline revalue
teunbrand Aug 2, 2024
3232471
simplify `scale_flip_position()`
teunbrand Aug 2, 2024
37da401
remove `as.quoted()` (note there is still `as_quoted()`)
teunbrand Aug 2, 2024
6ac62cf
replace `simplify_formula()` with `simplify()`
teunbrand Aug 2, 2024
db6feb8
inline `single_value()`
teunbrand Aug 2, 2024
e75c6d6
inline `update_guides()`
teunbrand Aug 2, 2024
4739a12
inline `is_column_vec()` and better name for `validate_column_vec()`
teunbrand Aug 2, 2024
47e36e2
remove/replace `wrap_as_facets_list()` (by `compact_facets()`
teunbrand Aug 2, 2024
d74f6a5
finishing touches
teunbrand Aug 2, 2024
8621926
resolve merge conflicts
teunbrand Aug 27, 2024
2f1f42f
`parse_axes_labeling` uses parent call
teunbrand Aug 27, 2024
276e6a2
elaborate on regex pattern
teunbrand Aug 27, 2024
2978d4d
Revert "inline `is_labeller()`"
teunbrand Aug 27, 2024
037c1ae
Revert "inline `is.sec_axis()`"
teunbrand Aug 27, 2024
8076706
Revert "replace single use internal functions"
teunbrand Aug 27, 2024
049db27
Collection of test functions for user-facing components
teunbrand Aug 27, 2024
ae2561b
apply tests when applicable
teunbrand Aug 27, 2024
3be8e6d
redocument
teunbrand Aug 27, 2024
13b8d36
abolish `uniquecols()`'s rownames
teunbrand Sep 13, 2024
d7092aa
move `is.*()` functions to class definitions
teunbrand Sep 13, 2024
16b35e9
resolve merge conflict
teunbrand Sep 13, 2024
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
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,20 @@ export(guide_transform)
export(guides)
export(has_flipped_aes)
export(is.Coord)
export(is.coord)
export(is.element)
export(is.facet)
export(is.geom)
export(is.ggplot)
export(is.ggproto)
export(is.guide)
export(is.guides)
export(is.layer)
export(is.mapping)
export(is.margin)
export(is.position)
export(is.scale)
export(is.stat)
export(is.theme)
export(label_both)
export(label_bquote)
Expand Down
2 changes: 2 additions & 0 deletions R/aes-evaluation.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ stage_scaled <- function(start = NULL, after_stat = NULL, after_scale = NULL) {
}

# Regex to determine if an identifier refers to a calculated aesthetic
teunbrand marked this conversation as resolved.
Show resolved Hide resolved
# The pattern includes ye olde '...var...' syntax, which was
# deprecated in 3.4.0 in favour of `after_stat()`
match_calculated_aes <- "^\\.\\.([a-zA-Z._]+)\\.\\.$"

# Determine if aesthetic is calculated
Expand Down
6 changes: 5 additions & 1 deletion R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ dup_axis <- function(transform = identity, name = derive(), breaks = derive(),
sec_axis(transform, trans = trans, name, breaks, labels, guide)
}

is.sec_axis <- function(x) {
inherits(x, "AxisSecondary")
}

set_sec_axis <- function(sec.axis, scale) {
if (!is.waive(sec.axis)) {
if (scale$is_discrete()) {
Expand All @@ -132,7 +136,7 @@ set_sec_axis <- function(sec.axis, scale) {
}
}
if (is.formula(sec.axis)) sec.axis <- sec_axis(sec.axis)
if (!inherits(sec.axis, "AxisSecondary")) {
if (!is.sec_axis(sec.axis)) {
cli::cli_abort("Secondary axes must be specified using {.fn sec_axis}.")
}
scale$secondary.axis <- sec.axis
Expand Down
4 changes: 3 additions & 1 deletion R/bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ bins <- function(breaks, closed = "right",
)
}

is_bins <- function(x) inherits(x, "ggplot2_bins")

#' @export
print.ggplot2_bins <- function(x, ...) {
n <- length(x$breaks)
Expand Down Expand Up @@ -131,7 +133,7 @@ bin_breaks_bins <- function(x_range, bins = 30, center = NULL,
# Compute bins ------------------------------------------------------------

bin_vector <- function(x, bins, weight = NULL, pad = FALSE) {
check_inherits(bins, "ggplot2_bins", "a {.cls ggplot2_bins} object")
check_object(bins, is_bins, "a {.cls ggplot2_bins} object")

if (all(is.na(x))) {
return(bin_out(length(x), NA, NA, xmin = NA, xmax = NA))
Expand Down
6 changes: 0 additions & 6 deletions R/coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,6 @@ Coord <- ggproto("Coord",
}
)

#' Is this object a coordinate system?
#'
#' @export is.Coord
#' @keywords internal
is.Coord <- function(x) inherits(x, "Coord")

# Renders an axis with the correct orientation or zeroGrob if no axis should be
# generated
render_axis <- function(panel_params, axis, scale, position, theme) {
Expand Down
2 changes: 1 addition & 1 deletion R/coord-cartesian-.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ view_scales_from_scale <- function(scale, coord_limits = NULL, expand = TRUE) {
}

panel_guides_grob <- function(guides, position, theme, labels = NULL) {
if (!inherits(guides, "Guides")) {
if (!is.guides(guides)) {
return(zeroGrob())
}
pair <- guides$get_position(position)
Expand Down
4 changes: 2 additions & 2 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,12 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
)
}

parse_axes_labeling <- function(x) {
parse_axes_labeling <- function(x, call = caller_env()) {
if (is.character(x)) {
x <- unlist(strsplit(x, ""))
x <- list(top = x[1], right = x[2], bottom = x[3], left = x[4])
} else if (!is.list(x)) {
cli::cli_abort("Panel labeling format not recognized.")
cli::cli_abort("Panel labeling format not recognized.", call = call)
}
x
}
Expand Down
9 changes: 1 addition & 8 deletions R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,6 @@ get_strip_labels <- function(plot = get_last_plot()) {
plot$plot$facet$format_strip_labels(layout, params)
}

#' Is this object a faceting specification?
#'
#' @param x object to test
#' @keywords internal
#' @export
is.facet <- function(x) inherits(x, "Facet")

# A "special" value, currently not used but could be used to determine
# if faceting is active
NO_PANEL <- -1L
Expand Down Expand Up @@ -444,7 +437,7 @@ as_facets_list <- function(x) {
}

validate_facets <- function(x) {
if (inherits(x, "uneval")) {
if (is.mapping(x)) {
cli::cli_abort("Please use {.fn vars} to supply facet variables.")
}
# Native pipe have higher precedence than + so any type of gg object can be
Expand Down
2 changes: 1 addition & 1 deletion R/fortify.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fortify.default <- function(model, data, ...) {
"or an object coercible by {.fn fortify}, or a valid ",
"{.cls data.frame}-like object coercible by {.fn as.data.frame}"
)
if (inherits(model, "uneval")) {
if (is.mapping(model)) {
msg <- c(
paste0(msg, ", not ", obj_type_friendly(model), "."),
"i" = "Did you accidentally pass {.fn aes} to the {.arg data} argument?"
Expand Down
2 changes: 1 addition & 1 deletion R/geom-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ get_geom_defaults <- function(geom, theme = theme_get()) {
if (is.character(geom)) {
geom <- check_subclass(geom, "Geom")
}
if (inherits(geom, "Geom")) {
if (is.geom(geom)) {
out <- geom$use_defaults(data = NULL, theme = theme)
return(out)
}
Expand Down
2 changes: 1 addition & 1 deletion R/geom-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ GeomLabel <- ggproto("GeomLabel", Geom,
data <- coord$transform(data, panel_params)
data$vjust <- compute_just(data$vjust, data$y, data$x, data$angle)
data$hjust <- compute_just(data$hjust, data$x, data$y, data$angle)
if (!inherits(label.padding, "margin")) {
if (!is.margin("margin")) {
label.padding <- rep(label.padding, length.out = 4)
}

Expand Down
5 changes: 0 additions & 5 deletions R/ggproto.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ ggproto_parent <- function(parent, self) {
structure(list(parent = parent, self = self), class = "ggproto_parent")
}

#' @param x An object to test.
#' @export
#' @rdname ggproto
is.ggproto <- function(x) inherits(x, "ggproto")

fetch_ggproto <- function(x, name) {
res <- NULL

Expand Down
2 changes: 1 addition & 1 deletion R/guide-.R
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@
# Renders tickmarks
build_ticks = function(key, elements, params, position = params$position,
length = elements$ticks_length) {
if (!inherits(elements, "element")) {
if (!is.element(elements)) {
elements <- elements$ticks
}
if (!inherits(elements, "element_line")) {
Expand Down Expand Up @@ -520,7 +520,7 @@
bottom = "top",
left = "right",
right = "left",
position

Check warning on line 523 in R/guide-.R

View check run for this annotation

Codecov / codecov/patch

R/guide-.R#L523

Added line #L523 was not covered by tests
)
}

Expand Down
6 changes: 3 additions & 3 deletions R/guides-.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
guides <- function(...) {
args <- list2(...)
if (length(args) > 0) {
if (is.list(args[[1]]) && !inherits(args[[1]], "guide")) args <- args[[1]]
if (is.list(args[[1]]) && !is.guide(args[[1]])) args <- args[[1]]

Check warning on line 72 in R/guides-.R

View check run for this annotation

Codecov / codecov/patch

R/guides-.R#L72

Added line #L72 was not covered by tests
args <- rename_aes(args)
}

Expand Down Expand Up @@ -138,7 +138,7 @@
if (is.null(guides)) {
return(invisible())
}
if (inherits(guides, "Guides")) {
if (is.guides(guides)) {
guides <- guides$guides
}
self$guides <- defaults(guides, self$guides)
Expand Down Expand Up @@ -862,7 +862,7 @@
guide <- fun()
}
}
if (inherits(guide, "Guide")) {
if (is.guide(guide)) {
return(guide)
}
if (inherits(guide, "guide") && is.list(guide)) {
Expand Down
4 changes: 3 additions & 1 deletion R/labeller.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ label_wrap_gen <- function(width = 25, multi_line = TRUE) {
structure(fun, class = "labeller")
}

is_labeller <- function(x) inherits(x, "labeller")

resolve_labeller <- function(rows, cols, labels) {
if (is.null(cols) && is.null(rows)) {
cli::cli_abort("Supply one of {.arg rows} or {.arg cols}.")
Expand Down Expand Up @@ -289,7 +291,7 @@ as_labeller <- function(x, default = label_value, multi_line = TRUE) {
# support it.
default <- dispatch_args(default, multi_line = multi_line)

if (inherits(x, "labeller")) {
if (is_labeller(x)) {
x <- dispatch_args(x, multi_line = multi_line)
x(labels)
} else if (is.function(x)) {
Expand Down
6 changes: 3 additions & 3 deletions R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
#' `NA`, the default, includes if any aesthetics are mapped.
#' `FALSE` never includes, and `TRUE` always includes.
#' It can also be a named logical vector to finely select the aesthetics to
#' display. To include legend keys for all levels, even
#' when no data exists, use `TRUE`. If `NA`, all levels are shown in legend,
#' display. To include legend keys for all levels, even
#' when no data exists, use `TRUE`. If `NA`, all levels are shown in legend,
#' but unobserved levels are omitted.
#' @param inherit.aes If `FALSE`, overrides the default aesthetics,
#' rather than combining with them. This is most useful for helper functions
Expand Down Expand Up @@ -203,7 +203,7 @@ layer <- function(geom = NULL, stat = NULL,
}

validate_mapping <- function(mapping, call = caller_env()) {
if (!inherits(mapping, "uneval")) {
if (!is.mapping(mapping)) {
msg <- "{.arg mapping} must be created by {.fn aes}."
# Native pipe have higher precedence than + so any type of gg object can be
# expected here, not just ggplot
Expand Down
2 changes: 1 addition & 1 deletion R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ ggplot_gtable.ggplot_built <- function(data) {
plot_margin <- calc_element("plot.margin", theme)
plot_table <- gtable_add_padding(plot_table, plot_margin)

if (inherits(theme$plot.background, "element")) {
if (is.element(theme$plot.background)) {
plot_table <- gtable_add_grob(plot_table,
element_render(theme, "plot.background"),
t = 1, l = 1, b = -1, r = -1, name = "background", z = -Inf)
Expand Down
2 changes: 1 addition & 1 deletion R/plot-construction.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@
}
#' @export
ggplot_add.Guides <- function(object, plot, object_name) {
if (inherits(plot$guides, "Guides")) {
if (is.guides(plot$guides)) {
# We clone the guides object to prevent modify-in-place of guides
old <- plot$guides
new <- ggproto(NULL, old)
new$add(object)
plot$guides <- new
} else {
plot$guides <- object

Check warning on line 159 in R/plot-construction.R

View check run for this annotation

Codecov / codecov/patch

R/plot-construction.R#L159

Added line #L159 was not covered by tests
}
plot
}
Expand Down
8 changes: 1 addition & 7 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ ggplot <- function(data = NULL, mapping = aes(), ...,
#' @export
ggplot.default <- function(data = NULL, mapping = aes(), ...,
environment = parent.frame()) {
if (!missing(mapping) && !inherits(mapping, "uneval")) {
if (!missing(mapping) && !is.mapping(mapping)) {
cli::cli_abort(c(
"{.arg mapping} must be created with {.fn aes}.",
"x" = "You've supplied {.obj_type_friendly {mapping}}."
Expand Down Expand Up @@ -154,12 +154,6 @@ plot_clone <- function(plot) {
p
}

#' Reports whether x is a ggplot object
#' @param x An object to test
#' @keywords internal
#' @export
is.ggplot <- function(x) inherits(x, "ggplot")

#' Explicitly draw plot
#'
#' Generally, you do not need to print or plot a ggplot2 plot explicitly: the
Expand Down
2 changes: 1 addition & 1 deletion R/scale-colour.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ scale_fill_binned <- function(...,
# helper function to make sure that the provided scale is of the correct
# type (i.e., is continuous and works with the provided aesthetic)
check_scale_type <- function(scale, name, aesthetic, scale_is_discrete = FALSE, call = caller_env()) {
if (!is.ggproto(scale) || !inherits(scale, "Scale")) {
if (!is.ggproto(scale) || !is.scale(scale)) {
cli::cli_abort(c(
"The {.arg type} argument must return a continuous scale for the {.field {aesthetic}} aesthetic.",
"x" = "The provided object is not a scale function."
Expand Down
10 changes: 2 additions & 8 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ theme <- function(...,
elements$panel.spacing.y <- elements$panel.margin.y
elements$panel.margin.y <- NULL
}
if (is.unit(elements$legend.margin) && !inherits(elements$legend.margin, "margin")) {
if (is.unit(elements$legend.margin) && !is.margin(elements$legend.margin)) {
cli::cli_warn(c(
"{.var legend.margin} must be specified using {.fn margin}",
"i" = "For the old behavior use {.var legend.spacing}"
Expand Down Expand Up @@ -529,7 +529,7 @@ theme <- function(...,
# If complete theme set all non-blank elements to inherit from blanks
if (complete) {
elements <- lapply(elements, function(el) {
if (inherits(el, "element") && !inherits(el, "element_blank")) {
if (is.element(el) && !inherits(el, "element_blank")) {
el$inherit.blank <- TRUE
}
el
Expand Down Expand Up @@ -905,12 +905,6 @@ combine_elements <- function(e1, e2) {
e1
}

#' Reports whether x is a theme object
#' @param x An object to test
#' @export
#' @keywords internal
is.theme <- function(x) inherits(x, "theme")

#' @export
`$.theme` <- function(x, ...) {
.subset2(x, ...)
Expand Down
Loading
Loading