Skip to content

Ensure ggproto methods start with { #6460

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 15 additions & 5 deletions R/coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ Coord <- ggproto("Coord",
# Should any of the scales be reversed?
reverse = "none",

aspect = function(ranges) NULL,
aspect = function(ranges) {
NULL
},

labels = function(self, labels, panel_params) {
labels
Expand Down Expand Up @@ -177,15 +179,23 @@ Coord <- ggproto("Coord",
panel_params
},

transform = function(data, range) NULL,
transform = function(data, range) {
NULL
},

distance = function(x, y, panel_params) NULL,
distance = function(x, y, panel_params) {
NULL
},

is_linear = function() FALSE,
is_linear = function() {
FALSE
},

# Does the coordinate system support free scaling of axes in a faceted plot?
# Will generally have to return FALSE for coordinate systems that enforce a fixed aspect ratio.
is_free = function() FALSE,
is_free = function() {
FALSE
},

setup_params = function(self, data) {
list(expand = parse_coord_expand(self$expand %||% TRUE))
Expand Down
9 changes: 7 additions & 2 deletions R/coord-cartesian-.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ coord_cartesian <- function(xlim = NULL, ylim = NULL, expand = TRUE,
#' @export
CoordCartesian <- ggproto("CoordCartesian", Coord,

is_linear = function() TRUE,
is_free = function() TRUE,
is_linear = function() {
TRUE
},

is_free = function() {
TRUE
},

distance = function(x, y, panel_params) {
max_dist <- dist_euclidean(panel_params$x$dimension(), panel_params$y$dimension())
Expand Down
4 changes: 3 additions & 1 deletion R/coord-fixed.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ coord_equal <- coord_fixed
#' @usage NULL
#' @export
CoordFixed <- ggproto("CoordFixed", CoordCartesian,
is_free = function() FALSE,
is_free = function() {
FALSE
},

aspect = function(self, ranges) {
diff(ranges$y.range) / diff(ranges$x.range) * self$ratio
Expand Down
8 changes: 6 additions & 2 deletions R/coord-polar.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ coord_polar <- function(theta = "x", start = 0, direction = 1, clip = "on") {
#' @export
CoordPolar <- ggproto("CoordPolar", Coord,

aspect = function(details) 1,
aspect = function(details) {
1
},

is_free = function() TRUE,
is_free = function() {
TRUE
},

distance = function(self, x, y, details, boost = 0.75) {
arc <- self$start + c(0, 2 * pi)
Expand Down
4 changes: 3 additions & 1 deletion R/coord-radial.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ CoordRadial <- ggproto("CoordRadial", Coord,
diff(details$bbox$y) / diff(details$bbox$x)
},

is_free = function() TRUE,
is_free = function() {
TRUE
},

distance = function(self, x, y, details, boost = 0.75) {
arc <- details$arc %||% c(0, 2 * pi)
Expand Down
12 changes: 9 additions & 3 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,15 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
},

# CoordSf enforces a fixed aspect ratio -> axes cannot be changed freely under faceting
is_free = function() FALSE,
is_free = function() {
FALSE
},

# for regular geoms (such as geom_path, geom_polygon, etc.), CoordSf is non-linear
# if the default_crs option is being used, i.e., not set to NULL
is_linear = function(self) is.null(self$get_default_crs()),
is_linear = function(self) {
is.null(self$get_default_crs())
},

distance = function(self, x, y, panel_params) {
d <- self$backtransform_range(panel_params)
Expand All @@ -327,7 +331,9 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
diff(panel_params$y_range) / diff(panel_params$x_range) / ratio
},

labels = function(labels, panel_params) labels,
labels = function(labels, panel_params) {
labels
},

render_bg = function(self, panel_params, theme) {
el <- calc_element("panel.grid.major", theme)
Expand Down
6 changes: 5 additions & 1 deletion R/coord-transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ coord_trans <- function(x = "identity", y = "identity", xlim = NULL, ylim = NULL
#' @usage NULL
#' @export
CoordTrans <- ggproto("CoordTrans", Coord,
is_free = function() TRUE,

is_free = function() {
TRUE
},

distance = function(self, x, y, panel_params) {
max_dist <- dist_euclidean(panel_params$x.range, panel_params$y.range)
dist_euclidean(self$trans$x$transform(x), self$trans$y$transform(y)) / max_dist
Expand Down
6 changes: 4 additions & 2 deletions R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ Facet <- ggproto("Facet", NULL,
map_data = function(data, layout, params) {
cli::cli_abort("Not implemented.")
},
setup_panel_params = function(self, panel_params, coord, ...) panel_params,
setup_panel_params = function(self, panel_params, coord, ...) {
panel_params
},
init_scales = function(layout, x_scale = NULL, y_scale = NULL, params) {
scales <- list()
if (!is.null(x_scale)) {
Expand Down Expand Up @@ -161,7 +163,7 @@ Facet <- ggproto("Facet", NULL,
params
)

# Draw individual panels, then call `$draw_panels()` method to
# Draw individual panels, then call `$draw_panels()` method to
# assemble into gtable
lapply(seq_along(panels[[1]]), function(i) {
panel <- lapply(panels, `[[`, i)
Expand Down
8 changes: 6 additions & 2 deletions R/geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ Geom <- ggproto("Geom",
cli::cli_abort("{.fn {snake_class(self)}}, has not implemented a {.fn draw_group} method")
},

setup_params = function(data, params) params,
setup_params = function(data, params) {
params
},

setup_data = function(data, params) data,
setup_data = function(data, params) {
data
},

# Combine data with defaults and set aesthetics from parameters
use_defaults = function(self, data, params = list(), modifiers = aes(),
Expand Down
8 changes: 6 additions & 2 deletions R/geom-blank.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ geom_blank <- function(mapping = NULL, data = NULL,
#' @export
GeomBlank <- ggproto("GeomBlank", Geom,
default_aes = aes(),
handle_na = function(data, params) data,
handle_na = function(data, params) {
data
},
check_constant_aes = FALSE,
draw_panel = function(...) nullGrob()
draw_panel = function(...) {
nullGrob()
}
)
20 changes: 15 additions & 5 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,9 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
n.breaks = NULL,
trans = transform_identity(),

is_discrete = function() FALSE,
is_discrete = function() {
FALSE
},

train = function(self, x) {
if (length(x) == 0) {
Expand Down Expand Up @@ -958,7 +960,9 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
n.breaks.cache = NULL,
palette.cache = NULL,

is_discrete = function() TRUE,
is_discrete = function() {
TRUE
},

train = function(self, x) {
if (length(x) == 0) {
Expand All @@ -979,7 +983,9 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
self$range$train(x, drop = self$drop, na.rm = !self$na.translate)
},

transform = identity,
transform = function(self, x) {
x
},

map = function(self, x, limits = self$get_limits()) {
limits <- vec_slice(limits, !is.na(limits))
Expand Down Expand Up @@ -1193,7 +1199,9 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
after.stat = FALSE,
show.limits = FALSE,

is_discrete = function() FALSE,
is_discrete = function() {
FALSE
},

train = function(self, x) {
if (!is.numeric(x)) {
Expand Down Expand Up @@ -1353,7 +1361,9 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
transformation$transform(breaks)
},

get_breaks_minor = function(...) NULL,
get_breaks_minor = function(...) {
NULL
},

get_labels = function(self, breaks = self$get_breaks()) {
if (is.null(breaks)) return(NULL)
Expand Down
28 changes: 21 additions & 7 deletions R/scale-view.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,27 @@ ViewScale <- ggproto("ViewScale", NULL,
is_empty = function(self) {
is.null(self$get_breaks()) && is.null(self$get_breaks_minor())
},
is_discrete = function(self) self$scale_is_discrete,
dimension = function(self) self$continuous_range,
get_limits = function(self) self$limits,
get_breaks = function(self) self$breaks,
get_breaks_minor = function(self) self$minor_breaks,
get_labels = function(self, breaks = self$get_breaks()) self$scale$get_labels(breaks),
get_transformation = function(self) self$scale$get_transformation(),
is_discrete = function(self) {
self$scale_is_discrete
},
dimension = function(self) {
self$continuous_range
},
get_limits = function(self) {
self$limits
},
get_breaks = function(self) {
self$breaks
},
get_breaks_minor = function(self) {
self$minor_breaks
},
get_labels = function(self, breaks = self$get_breaks()) {
self$scale$get_labels(breaks)
},
get_transformation = function(self) {
self$scale$get_transformation()
},
rescale = function(self, x) {
self$scale$rescale(x, self$limits, self$continuous_range)
},
Expand Down
4 changes: 3 additions & 1 deletion R/stat-unique.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ stat_unique <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
StatUnique <- ggproto("StatUnique", Stat,
compute_panel = function(data, scales) unique0(data)
compute_panel = function(data, scales) {
unique0(data)
}
)
21 changes: 21 additions & 0 deletions tests/testthat/test-ggproto.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,24 @@ test_that("construction checks input", {
expect_snapshot_error(ggproto("Test", NULL, a <- function(self, a) a))
expect_snapshot_error(ggproto("Test", mtcars, a = function(self, a) a))
})

test_that("all ggproto methods start with `{` (#6459)", {

ggprotos <- Filter(
function(x) inherits(x, "ggproto"),
mget(ls("package:ggplot2"), asNamespace("ggplot2"), ifnotfound = list(NULL))
)

method_nobrackets <- lapply(ggprotos, function(x) {
Filter(
function(m) inherits(x[[m]], "ggproto_method") && {
b <- as.list(body(get(m, x)))
length(b) == 0 || b[[1]] != quote(`{`)
},
ls(envir = x)
)
})

expect_length(Filter(length, method_nobrackets), 0)

})
Loading