Skip to content

Forward compatibility: is_*() functions #6388

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 21 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,21 @@ export(is.facet)
export(is.ggplot)
export(is.ggproto)
export(is.theme)
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)
export(label_context)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ggplot2 (development version)

* Standardised test functions for important classes: `is_ggproto()`,
`is_ggplot()`, `is_mapping()`, `is_layer()`, `is_geom()`, `is_stat()`,
`is_position()`, `is_coord()`, `is_facet()`, `is_scale()`, `is_guide()`,
`is_guides()`, `is_margin()`, `is_element()` and `is_theme()`.
* New `get_labs()` function for retrieving completed plot labels
(@teunbrand, #6008).
* New `get_geom_defaults()` for retrieving resolved default aesthetics.
Expand Down
4 changes: 4 additions & 0 deletions R/aes.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ aes <- function(x, y, ...) {
rename_aes(aes)
}

#' @export
#' @rdname is_tests
is_mapping <- function(x) inherits(x, "uneval")

# Wrap symbolic objects in quosures but pull out constants out of
# quosures for backward-compatibility
new_aesthetic <- function(x, env = globalenv()) {
Expand Down
16 changes: 11 additions & 5 deletions R/coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,17 @@ Coord <- ggproto("Coord",
}
)

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

#' @export
#' @rdname is_tests
#' @usage is.Coord(x) # Deprecated
is.Coord <- function(x) {
deprecate_soft0("3.5.2", "is.Coord()", "is_coord()")
is_coord(x)
}

# Renders an axis with the correct orientation or zeroGrob if no axis should be
# generated
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
16 changes: 10 additions & 6 deletions R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,17 @@ vars <- function(...) {
quos(...)
}

#' @export
#' @rdname is_tests
is_facet <- function(x) inherits(x, "Facet")

#' Is this object a faceting specification?
#'
#' @param x object to test
#' @keywords internal
#' @export
is.facet <- function(x) inherits(x, "Facet")
#' @rdname is_tests
#' @usage is.facet(x) # Deprecated
is.facet <- function(x) {
deprecate_soft0("3.5.2", "is.facet()", "is_facet()")
is_facet(x)
}

# A "special" value, currently not used but could be used to determine
# if faceting is active
Expand Down Expand Up @@ -324,7 +328,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 @@ -82,7 +82,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(
glue(msg0, ", not {obj_type_friendly(model)}."),
"i" = "Did you accidentally pass {.fn aes} to the {.arg data} argument?"
Expand Down
3 changes: 3 additions & 0 deletions R/geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ Geom <- ggproto("Geom",

)

#' @export
#' @rdname is_tests
is_geom <- function(x) inherits(x, "Geom")

#' Graphical units
#'
Expand Down
19 changes: 13 additions & 6 deletions R/ggproto.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#' self$x
#' }
#' )
#' is.ggproto(Adder)
#' is_ggproto(Adder)
#'
#' Adder$add(10)
#' Adder$add(10)
Expand Down Expand Up @@ -88,7 +88,7 @@ ggproto <- function(`_class` = NULL, `_inherit` = NULL, ...) {

super <- find_super()
if (!is.null(super)) {
check_object(super, is.ggproto, "a {.cls ggproto} object", arg = "_inherit")
check_object(super, is_ggproto, "a {.cls ggproto} object", arg = "_inherit")
e$super <- find_super
class(e) <- c(`_class`, class(super))
} else {
Expand All @@ -106,10 +106,17 @@ 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")
#' @rdname is_tests
is_ggproto <- function(x) inherits(x, "ggproto")

#' @export
#' @rdname is_tests
#' @usage is.ggproto(x) # Deprecated
is.ggproto <- function(x) {
deprecate_soft0("3.5.2", "is.ggproto()", "is_ggproto()")
is_ggproto(x)
}

fetch_ggproto <- function(x, name) {
res <- NULL
Expand Down Expand Up @@ -305,7 +312,7 @@ object_summaries <- function(x, exclude = NULL, flat = TRUE) {
values <- vapply(obj_names, function(name) {
obj <- x[[name]]
if (is.function(obj)) "function"
else if (is.ggproto(obj)) format(obj, flat = flat)
else if (is_ggproto(obj)) format(obj, flat = flat)
else if (is.environment(obj)) "environment"
else if (is.null(obj)) "NULL"
else if (is.atomic(obj)) trim(paste(as.character(obj), collapse = " "))
Expand Down
10 changes: 7 additions & 3 deletions R/guide-.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ new_guide <- function(..., available_aes = "any", super) {

# Validate theme settings
if (!is.null(params$theme)) {
check_object(params$theme, is.theme, what = "a {.cls theme} object")
check_object(params$theme, is_theme, what = "a {.cls theme} object")
validate_theme(params$theme, call = caller_env())
params$direction <- params$direction %||% params$theme$legend.direction
}
Expand All @@ -66,6 +66,10 @@ new_guide <- function(..., available_aes = "any", super) {
)
}

#' @export
#' @rdname is_tests
is_guide <- function(x) inherits(x, "Guide")

#' @section Guides:
#'
#' The `guide_*()` functions, such as `guide_legend()` return an object that
Expand Down Expand Up @@ -377,10 +381,10 @@ Guide <- ggproto(
# 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")) {
if (!is_element(elements, "line")) {
return(zeroGrob())
}

Expand Down
2 changes: 1 addition & 1 deletion R/guide-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ GuideAxis <- ggproto(

override_elements = function(params, elements, theme) {
label <- elements$text
if (!inherits(label, "element_text")) {
if (!is_element(label, "text")) {
return(elements)
}
label_overrides <- axis_label_element_overrides(
Expand Down
2 changes: 1 addition & 1 deletion R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ deprecated_guide_args <- function(

# Set as theme
theme <- compact(theme)
if (!is.theme(theme)) {
if (!is_theme(theme)) {
theme <- inject(theme(!!!theme))
}
theme
Expand Down
10 changes: 7 additions & 3 deletions R/guides-.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ guides <- function(...) {
NULL
}

#' @export
#' @rdname is_tests
is_guides <- function(x) inherits(x, "Guides")

update_guides <- function(p, guides) {
p <- plot_clone(p)
if (inherits(p$guides, "Guides")) {
if (is_guides(p$guides)) {
old <- p$guides
new <- ggproto(NULL, old)
new$add(guides)
Expand Down Expand Up @@ -151,7 +155,7 @@ Guides <- ggproto(
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 @@ -898,7 +902,7 @@ validate_guide <- function(guide) {
guide <- fun()
}
}
if (inherits(guide, "Guide")) {
if (is_guide(guide)) {
return(guide)
}
if (inherits(guide, "guide") && is.list(guide)) {
Expand Down
6 changes: 4 additions & 2 deletions R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,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 Expand Up @@ -462,7 +462,9 @@ Layer <- ggproto("Layer", NULL,
}
)

is.layer <- function(x) inherits(x, "Layer")
#' @export
#' @rdname is_tests
is_layer <- function(x) inherits(x, "Layer")

check_subclass <- function(x, subclass,
argname = to_lower_ascii(subclass),
Expand Down
5 changes: 4 additions & 1 deletion R/margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ margin <- function(t = 0, r = 0, b = 0, l = 0, unit = "pt") {
class(u) <- c("margin", class(u))
u
}
is.margin <- function(x) {

#' @export
#' @rdname is_tests
is_margin <- function(x) {
inherits(x, "margin")
}

Expand Down
2 changes: 1 addition & 1 deletion R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ ggplot_gtable.ggplot_built <- function(data) {
plot_table <- gtable_add_rows(plot_table, theme$plot.margin[3])
plot_table <- gtable_add_cols(plot_table, theme$plot.margin[4], pos = 0)

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
6 changes: 3 additions & 3 deletions R/plot-construction.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
# can be displayed in error messages
e2name <- deparse(substitute(e2))

if (is.theme(e1)) add_theme(e1, e2, e2name)
else if (is.ggplot(e1)) add_ggplot(e1, e2, e2name)
else if (is.ggproto(e1)) {
if (is_theme(e1)) add_theme(e1, e2, e2name)
else if (is_ggplot(e1)) add_ggplot(e1, e2, e2name)
else if (is_ggproto(e1)) {
cli::cli_abort(c(
"Cannot add {.cls ggproto} objects together.",
"i" = "Did you forget to add this object to a {.cls ggplot} object?"
Expand Down
15 changes: 12 additions & 3 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 @@ -156,11 +156,20 @@ plot_clone <- function(plot) {
p
}

#' Reports whether x is a ggplot object
#' Reports wether `x` is a type of object
#' @param x An object to test
#' @keywords internal
#' @export
is.ggplot <- function(x) inherits(x, "ggplot")
#' @name is_tests
is_ggplot <- function(x) inherits(x, "ggplot")

#' @export
#' @rdname is_tests
#' @usage is.ggplot(x) # Deprecated
is.ggplot <- function(x) {
deprecate_soft0("3.5.2", "is.ggplot", "is_ggplot")
is_ggplot(x)
}

#' Explicitly draw plot
#'
Expand Down
4 changes: 4 additions & 0 deletions R/position-.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Position <- ggproto("Position",
}
)

#' @export
#' @rdname is_tests
is_position <- function(x) inherits(x, "Position")

#' Convenience function to transform all position variables.
#'
#' @param trans_x,trans_y Transformation functions for x and y aesthetics.
Expand Down
4 changes: 4 additions & 0 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name =
)
}

#' @export
#' @rdname is_tests
is_scale <- function(x) inherits(x, "Scale")

#' @section Scales:
#'
#' All `scale_*` functions like [scale_x_continuous()] return a `Scale*`
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
4 changes: 4 additions & 0 deletions R/stat-.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,7 @@ Stat <- ggproto("Stat",
}

)

#' @export
#' @rdname is_tests
is_stat <- function(x) inherits(x, "Stat")
Loading