Skip to content

Commit ba21a7b

Browse files
authored
Streamline S7 parts (#6546)
* remove `build_ggplot()` in favour of `ggplot_build()` * remove `gtable_ggplot()` in favour of `ggplot_gtable()` * remove `draw_element()` in favour of `element_grob()` * Use `S7::method()` for element getters/setters * Use `S7::method()` for gg getters/setters * Use `S7::method()` for class_mapping getters/setters * Use `S7::method()` for class_theme getter * Use `S7::method(print)` for S7 classes * revert replacement functions * damn me and my stupid decisions sometimes * sprinkle comments
1 parent b77aba2 commit ba21a7b

File tree

11 files changed

+139
-202
lines changed

11 files changed

+139
-202
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ Collate:
9393
'aes-group-order.R'
9494
'aes-linetype-size-shape.R'
9595
'aes-position.R'
96+
'all-classes.R'
9697
'compat-plyr.R'
9798
'utilities.R'
9899
'aes.R'
99-
'all-classes.R'
100100
'annotation-borders.R'
101101
'utilities-checks.R'
102102
'legend-draw.R'

NAMESPACE

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
S3method("$","ggplot2::element")
4-
S3method("$","ggplot2::gg")
5-
S3method("$","ggplot2::theme")
63
S3method("$",ggproto)
74
S3method("$",ggproto_parent)
85
S3method("$<-","ggplot2::element")
96
S3method("$<-","ggplot2::gg")
107
S3method("$<-","ggplot2::mapping")
11-
S3method("[","ggplot2::element")
12-
S3method("[","ggplot2::gg")
13-
S3method("[","ggplot2::mapping")
148
S3method("[",mapped_discrete)
159
S3method("[<-","ggplot2::element")
1610
S3method("[<-","ggplot2::gg")
1711
S3method("[<-","ggplot2::mapping")
1812
S3method("[<-",mapped_discrete)
19-
S3method("[[","ggplot2::element")
20-
S3method("[[","ggplot2::gg")
2113
S3method("[[",ggproto)
2214
S3method("[[<-","ggplot2::element")
2315
S3method("[[<-","ggplot2::gg")
@@ -29,7 +21,6 @@ S3method(autolayer,default)
2921
S3method(autoplot,default)
3022
S3method(c,mapped_discrete)
3123
S3method(drawDetails,zeroGrob)
32-
S3method(element_grob,default)
3324
S3method(format,ggproto)
3425
S3method(format,ggproto_method)
3526
S3method(format,rd_section_aesthetics)
@@ -59,8 +50,6 @@ S3method(fortify,tbl_df)
5950
S3method(ggplot,"function")
6051
S3method(ggplot,default)
6152
S3method(ggplot_add,default)
62-
S3method(ggplot_build,default)
63-
S3method(ggplot_gtable,default)
6453
S3method(grid.draw,absoluteGrob)
6554
S3method(grobHeight,absoluteGrob)
6655
S3method(grobHeight,zeroGrob)
@@ -92,10 +81,6 @@ S3method(predictdf,default)
9281
S3method(predictdf,glm)
9382
S3method(predictdf,locfit)
9483
S3method(predictdf,loess)
95-
S3method(print,"ggplot2::ggplot")
96-
S3method(print,"ggplot2::mapping")
97-
S3method(print,"ggplot2::theme")
98-
S3method(print,element)
9984
S3method(print,ggplot2_bins)
10085
S3method(print,ggproto)
10186
S3method(print,ggproto_method)
@@ -292,7 +277,6 @@ export(autoplot)
292277
export(benchplot)
293278
export(binned_scale)
294279
export(borders)
295-
export(build_ggplot)
296280
export(calc_element)
297281
export(check_device)
298282
export(class_S3_gg)
@@ -336,7 +320,6 @@ export(cut_width)
336320
export(datetime_scale)
337321
export(derive)
338322
export(discrete_scale)
339-
export(draw_element)
340323
export(draw_key_abline)
341324
export(draw_key_blank)
342325
export(draw_key_boxplot)
@@ -460,7 +443,6 @@ export(ggproto)
460443
export(ggproto_parent)
461444
export(ggsave)
462445
export(ggtitle)
463-
export(gtable_ggplot)
464446
export(guide_axis)
465447
export(guide_axis_logticks)
466448
export(guide_axis_stack)

R/aes.R

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' @include utilities.R compat-plyr.R
1+
#' @include utilities.R compat-plyr.R all-classes.R
22
NULL
33

44
#' Construct aesthetic mappings
@@ -130,41 +130,42 @@ new_aesthetic <- function(x, env = globalenv()) {
130130
x
131131
}
132132

133+
# TODO: remove `local()` when S7 has fixed S7/#390
133134
#' @export
134-
# TODO: should convert to proper S7 method once bug in S7 is resolved
135-
`print.ggplot2::mapping` <- function(x, ...) {
136-
cat("Aesthetic mapping: \n")
135+
local({
136+
S7::method(print, class_mapping) <- function(x, ...) {
137+
cat("Aesthetic mapping: \n")
137138

138-
if (length(x) == 0) {
139-
cat("<empty>\n")
140-
} else {
141-
values <- vapply(x, quo_label, character(1))
142-
bullets <- paste0("* ", format(paste0("`", names(x), "`")), " -> ", values, "\n")
139+
if (length(x) == 0) {
140+
cat("<empty>\n")
141+
} else {
142+
values <- vapply(x, quo_label, character(1))
143+
bullets <- paste0("* ", format(paste0("`", names(x), "`")), " -> ", values, "\n")
143144

144-
cat(bullets, sep = "")
145+
cat(bullets, sep = "")
146+
}
147+
148+
invisible(x)
145149
}
150+
})
146151

147-
invisible(x)
148-
}
152+
local({
153+
S7::method(`[`, class_mapping) <- function(x, i, ...) {
154+
class_mapping(`[`(S7::S7_data(x), i, ...))
155+
}
156+
})
149157

150-
# TODO: should convert to proper S7 method once bug in S7 is resolved
151158
#' @export
152-
"[.ggplot2::mapping" <- function(x, i, ...) {
153-
class_mapping(NextMethod())
159+
`[[<-.ggplot2::mapping` <- function(x, i, value) {
160+
class_mapping(`[[<-`(S7::S7_data(x), i, value))
154161
}
155162

156-
# If necessary coerce replacements to quosures for compatibility
157-
#' @export
158-
"[[<-.ggplot2::mapping" <- function(x, i, value) {
159-
class_mapping(NextMethod())
160-
}
161163
#' @export
162-
"$<-.ggplot2::mapping" <- function(x, i, value) {
163-
class_mapping(NextMethod())
164-
}
164+
`$<-.ggplot2::mapping` <- `[[<-.ggplot2::mapping`
165+
165166
#' @export
166-
"[<-.ggplot2::mapping" <- function(x, i, value) {
167-
class_mapping(NextMethod())
167+
`[<-.ggplot2::mapping` <- function(x, i, value) {
168+
class_mapping(`[<-`(S7::S7_data(x), i, value))
168169
}
169170

170171
#' Standardise aesthetic names

R/plot-build.R

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,22 @@
2525
#' The `r link_book("build step section", "internals#sec-ggplotbuild")`
2626
#' @keywords internal
2727
#' @export
28-
build_ggplot <- S7::new_generic("build_ggplot", "plot", fun = function(plot, ...) {
28+
ggplot_build <- function(plot, ...) {
29+
# TODO: Swap to S7 generic once S7/#543 is resolved
2930
env <- try_prop(plot, "plot_env")
3031
if (!is.null(env)) {
3132
attach_plot_env(env)
3233
}
33-
S7::S7_dispatch()
34-
})
34+
UseMethod("ggplot_build")
35+
}
3536

36-
S7::method(build_ggplot, class_ggplot_built) <- function(plot, ...) {
37+
S7::method(ggplot_build, class_ggplot_built) <- function(plot, ...) {
3738
plot # This is a no-op
3839
}
3940

40-
S7::method(build_ggplot, class_ggplot) <- function(plot, ...) {
41+
# The build_ggplot is a temporary concession to {thematic} after we put in
42+
# a compatibility PR that uses this function
43+
build_ggplot <- S7::method(ggplot_build, class_ggplot) <- function(plot, ...) {
4144
plot <- plot_clone(plot)
4245
if (length(plot@layers) == 0) {
4346
plot <- plot + geom_blank()
@@ -136,29 +139,17 @@ S7::method(build_ggplot, class_ggplot) <- function(plot, ...) {
136139
build
137140
}
138141

139-
# TODO: the S3 generic should be phased out once S7 is adopted more widely
140-
#' @rdname build_ggplot
141-
#' @export
142-
ggplot_build <- function(plot, ...) {
143-
UseMethod("ggplot_build")
144-
}
145-
146-
#' @export
147-
ggplot_build.default <- function(plot, ...) {
148-
build_ggplot(plot)
149-
}
150-
151142
#' @export
152-
#' @rdname build_ggplot
143+
#' @rdname ggplot_build
153144
get_layer_data <- function(plot = get_last_plot(), i = 1L) {
154145
ggplot_build(plot)@data[[i]]
155146
}
156147
#' @export
157-
#' @rdname build_ggplot
148+
#' @rdname ggplot_build
158149
layer_data <- get_layer_data
159150

160151
#' @export
161-
#' @rdname build_ggplot
152+
#' @rdname ggplot_build
162153
get_panel_scales <- function(plot = get_last_plot(), i = 1L, j = 1L) {
163154
b <- ggplot_build(plot)
164155

@@ -172,19 +163,19 @@ get_panel_scales <- function(plot = get_last_plot(), i = 1L, j = 1L) {
172163
}
173164

174165
#' @export
175-
#' @rdname build_ggplot
166+
#' @rdname ggplot_build
176167
layer_scales <- get_panel_scales
177168

178169
#' @export
179-
#' @rdname build_ggplot
170+
#' @rdname ggplot_build
180171
get_layer_grob <- function(plot = get_last_plot(), i = 1L) {
181172
b <- ggplot_build(plot)
182173

183174
b@plot@layers[[i]]$draw_geom(b@data[[i]], b@layout)
184175
}
185176

186177
#' @export
187-
#' @rdname build_ggplot
178+
#' @rdname ggplot_build
188179
layer_grob <- get_layer_grob
189180

190181
#' Build a plot with all the usual bits and pieces.
@@ -208,12 +199,13 @@ layer_grob <- get_layer_grob
208199
#' @keywords internal
209200
#' @param data plot data generated by [ggplot_build()]
210201
#' @export
211-
gtable_ggplot <- S7::new_generic("gtable_ggplot", "data", function(data) {
202+
ggplot_gtable <- function(data) {
203+
# TODO: Swap to S7 generic once S7/#543 is resolved
212204
attach_plot_env(data@plot@plot_env)
213-
S7::S7_dispatch()
214-
})
205+
UseMethod("ggplot_gtable")
206+
}
215207

216-
S7::method(gtable_ggplot, class_ggplot_built) <- function(data) {
208+
S7::method(ggplot_gtable, class_ggplot_built) <- function(data) {
217209
plot <- data@plot
218210
layout <- data@layout
219211
data <- data@data
@@ -314,18 +306,6 @@ S7::method(gtable_ggplot, class_ggplot_built) <- function(data) {
314306
plot_table
315307
}
316308

317-
# TODO: the S3 generic should be phased out once S7 is adopted more widely
318-
#' @rdname gtable_ggplot
319-
#' @export
320-
ggplot_gtable <- function(data) {
321-
UseMethod("ggplot_gtable")
322-
}
323-
324-
#' @export
325-
ggplot_gtable.default <- function(data) {
326-
gtable_ggplot(data)
327-
}
328-
329309
#' Generate a ggplot2 plot grob.
330310
#'
331311
#' @param x ggplot2 object

0 commit comments

Comments
 (0)