Skip to content

Commit 30b1118

Browse files
committed
Implement <ggplot_built> as S7
1 parent 2607597 commit 30b1118

32 files changed

+165
-155
lines changed

R/all-classes.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class_guides <- S7::new_S3_class("Guides")
55
class_coord <- S7::new_S3_class("Coord")
66
class_facet <- S7::new_S3_class("Facet")
77
class_layer <- S7::new_S3_class("Layer")
8+
class_layout <- S7::new_S3_class("Layout")
89
class_scales_list <- S7::new_S3_class("ScalesList")
910
class_layout <- S7::new_S3_class("Layout")
1011
class_ggproto <- S7::new_S3_class("ggproto")
12+
class_gtable <- S7::new_S3_class("gtable")

R/facet-.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ vars <- function(...) {
399399
#' get_strip_labels(p + facet_grid(year ~ cyl))
400400
get_strip_labels <- function(plot = get_last_plot()) {
401401
plot <- ggplot_build(plot)
402-
layout <- plot$layout$layout
403-
params <- plot$layout$facet_params
404-
plot$plot@facet$format_strip_labels(layout, params)
402+
layout <- plot@layout$layout
403+
params <- plot@layout$facet_params
404+
plot@plot@facet$format_strip_labels(layout, params)
405405
}
406406

407407
# A "special" value, currently not used but could be used to determine

R/guides-.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,20 +832,20 @@ get_guide_data <- function(plot = get_last_plot(), aesthetic, panel = 1L) {
832832

833833
if (!aesthetic %in% c("x", "y", "x.sec", "y.sec", "theta", "r")) {
834834
# Non position guides: check if aesthetic in colnames of key
835-
keys <- lapply(plot$plot@guides$params, `[[`, "key")
835+
keys <- lapply(plot@plot@guides$params, `[[`, "key")
836836
keep <- vapply(keys, function(x) any(colnames(x) %in% aesthetic), logical(1))
837837
keys <- switch(sum(keep) + 1, NULL, keys[[which(keep)]], keys[keep])
838838
return(keys)
839839
}
840840

841841
# Position guides: find the right layout entry
842842
check_number_whole(panel)
843-
layout <- plot$layout$layout
843+
layout <- plot@layout$layout
844844
select <- layout[layout$PANEL == panel, , drop = FALSE]
845845
if (nrow(select) == 0) {
846846
return(NULL)
847847
}
848-
params <- plot$layout$panel_params[select$PANEL][[1]]
848+
params <- plot@layout$panel_params[select$PANEL][[1]]
849849

850850
# If panel params don't have guides, we probably have old coord system
851851
# that doesn't use the guide system.

R/labels.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,18 @@ ggtitle <- function(label, subtitle = waiver()) {
220220
get_labs <- function(plot = get_last_plot()) {
221221
plot <- ggplot_build(plot)
222222

223-
labs <- plot$plot@labels
223+
labs <- plot@plot@labels
224224

225225
xy_labs <- rename(
226-
c(x = plot$layout$resolve_label(plot$layout$panel_scales_x[[1]], labs),
227-
y = plot$layout$resolve_label(plot$layout$panel_scales_y[[1]], labs)),
226+
c(x = plot@layout$resolve_label(plot@layout$panel_scales_x[[1]], labs),
227+
y = plot@layout$resolve_label(plot@layout$panel_scales_y[[1]], labs)),
228228
c(x.primary = "x", x.secondary = "x.sec",
229229
y.primary = "y", y.secondary = "y.sec")
230230
)
231231

232232
labs <- defaults(xy_labs, labs)
233233

234-
guides <- plot$plot@guides
234+
guides <- plot@plot@guides
235235
if (length(guides$aesthetics) == 0) {
236236
return(labs)
237237
}
@@ -287,14 +287,14 @@ get_alt_text.ggplot <- function(p, ...) {
287287
}
288288
p@labels[["alt"]] <- NULL
289289
build <- ggplot_build(p)
290-
build$plot@labels[["alt"]] <- alt
290+
build@plot@labels[["alt"]] <- alt
291291
get_alt_text(build)
292292
}
293293
#' @export
294294
get_alt_text.ggplot_built <- function(p, ...) {
295-
alt <- p$plot@labels[["alt"]] %||% ""
296-
p$plot@labels[["alt"]] <- NULL
297-
if (is.function(alt)) alt(p$plot) else alt
295+
alt <- p@plot@labels[["alt"]] %||% ""
296+
p@plot@labels[["alt"]] <- NULL
297+
if (is.function(alt)) alt(p@plot) else alt
298298
}
299299
#' @export
300300
get_alt_text.gtable <- function(p, ...) {

R/plot-build.R

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
#' @include plot.R
2+
3+
class_ggplot_built <- S7::new_class(
4+
"ggplot_built",
5+
properties = list(
6+
data = S7::class_list,
7+
layout = class_layout,
8+
plot = class_ggplot
9+
)
10+
)
11+
112
#' Build ggplot for rendering.
213
#'
314
#' `ggplot_build()` takes the plot object, and performs all steps necessary
@@ -131,16 +142,13 @@ ggplot_build.ggplot <- function(plot) {
131142
# Consolidate alt-text
132143
plot@labels$alt <- get_alt_text(plot)
133144

134-
structure(
135-
list(data = data, layout = layout, plot = plot),
136-
class = "ggplot_built"
137-
)
145+
class_ggplot_built(data = data, layout = layout, plot = plot)
138146
}
139147

140148
#' @export
141149
#' @rdname ggplot_build
142150
get_layer_data <- function(plot = get_last_plot(), i = 1L) {
143-
ggplot_build(plot)$data[[i]]
151+
ggplot_build(plot)@data[[i]]
144152
}
145153
#' @export
146154
#' @rdname ggplot_build
@@ -151,12 +159,12 @@ layer_data <- get_layer_data
151159
get_panel_scales <- function(plot = get_last_plot(), i = 1L, j = 1L) {
152160
b <- ggplot_build(plot)
153161

154-
layout <- b$layout$layout
162+
layout <- b@layout$layout
155163
selected <- layout[layout$ROW == i & layout$COL == j, , drop = FALSE]
156164

157165
list(
158-
x = b$layout$panel_scales_x[[selected$SCALE_X]],
159-
y = b$layout$panel_scales_y[[selected$SCALE_Y]]
166+
x = b@layout$panel_scales_x[[selected$SCALE_X]],
167+
y = b@layout$panel_scales_y[[selected$SCALE_Y]]
160168
)
161169
}
162170

@@ -169,7 +177,7 @@ layer_scales <- get_panel_scales
169177
get_layer_grob <- function(plot = get_last_plot(), i = 1L) {
170178
b <- ggplot_build(plot)
171179

172-
b$plot@layers[[i]]$draw_geom(b$data[[i]], b$layout)
180+
b@plot@layers[[i]]$draw_geom(b@data[[i]], b@layout)
173181
}
174182

175183
#' @export
@@ -203,9 +211,9 @@ ggplot_gtable <- function(data) {
203211

204212
#' @export
205213
ggplot_gtable.ggplot_built <- function(data) {
206-
plot <- data$plot
207-
layout <- data$layout
208-
data <- data$data
214+
plot <- data@plot
215+
layout <- data@layout
216+
data <- data@data
209217
theme <- plot@theme
210218

211219
geom_grobs <- by_layer(function(l, d) l$draw_geom(d, layout), plot@layers, data, "converting geom to grob")

R/summarise-plot.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ NULL
6262
#' @rdname summarise_plot
6363
#' @export
6464
summarise_layout <- function(p) {
65-
check_inherits(p, "ggplot_built")
66-
l <- p$layout
65+
check_inherits(p, "ggplot2::ggplot_built")
66+
l <- p@layout
6767

6868
layout <- l$layout
6969
layout <- data_frame0(
@@ -99,7 +99,7 @@ summarise_layout <- function(p) {
9999
#' @rdname summarise_plot
100100
#' @export
101101
summarise_coord <- function(p) {
102-
check_inherits(p, "ggplot_built")
102+
check_inherits(p, "ggplot2::ggplot_built")
103103

104104
# Given a transform object, find the log base; if the transform object is
105105
# NULL, or if it's not a log transform, return NA.
@@ -112,23 +112,23 @@ summarise_coord <- function(p) {
112112
}
113113

114114
list(
115-
xlog = trans_get_log_base(p$layout$coord$trans$x),
116-
ylog = trans_get_log_base(p$layout$coord$trans$y),
117-
flip = inherits(p$layout$coord, "CoordFlip")
115+
xlog = trans_get_log_base(p@layout$coord$trans$x),
116+
ylog = trans_get_log_base(p@layout$coord$trans$y),
117+
flip = inherits(p@layout$coord, "CoordFlip")
118118
)
119119
}
120120

121121

122122
#' @rdname summarise_plot
123123
#' @export
124124
summarise_layers <- function(p) {
125-
check_inherits(p, "ggplot_built")
125+
check_inherits(p, "ggplot2::ggplot_built")
126126

127127
# Default mappings. Make sure it's a regular list instead of a mapping
128128
# object.
129-
default_mapping <- unclass(p$plot@mapping)
129+
default_mapping <- unclass(p@plot@mapping)
130130

131-
layer_mappings <- lapply(p$plot@layers, function(layer) {
131+
layer_mappings <- lapply(p@plot@layers, function(layer) {
132132
defaults(layer$mapping, default_mapping)
133133
})
134134

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# summarise_*() throws appropriate errors
22

3-
`p` must be a <ggplot_built> object, not the number 10.
3+
`p` must be a <ggplot2::ggplot_built> object, not the number 10.
44

55
---
66

7-
`p` must be a <ggplot_built> object, not the string "A".
7+
`p` must be a <ggplot2::ggplot_built> object, not the string "A".
88

99
---
1010

11-
`p` must be a <ggplot_built> object, not `TRUE`.
11+
`p` must be a <ggplot2::ggplot_built> object, not `TRUE`.
1212

tests/testthat/helper-plot-data.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
cdata <- function(plot) {
33
pieces <- ggplot_build(plot)
44

5-
lapply(pieces$data, function(d) {
5+
lapply(pieces@data, function(d) {
66
dapply(d, "PANEL", function(panel_data) {
7-
scales <- pieces$layout$get_scales(panel_data$PANEL[1])
8-
panel_params <- plot@coordinates$setup_panel_params(scales$x, scales$y, params = pieces$layout$coord_params)
7+
scales <- pieces@layout$get_scales(panel_data$PANEL[1])
8+
panel_params <- plot@coordinates$setup_panel_params(scales$x, scales$y, params = pieces@layout$coord_params)
99
plot@coordinates$transform(panel_data, panel_params)
1010
})
1111
})
1212
}
1313

1414
pranges <- function(plot) {
15-
layout <- ggplot_build(plot)$layout
15+
layout <- ggplot_build(plot)@layout
1616

1717
x_ranges <- lapply(layout$panel_scales_x, function(scale) scale$get_limits())
1818
y_ranges <- lapply(layout$panel_scales_y, function(scale) scale$get_limits())

tests/testthat/test-aes.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ test_that("assignment methods pull unwrap constants from quosures", {
9696

9797
test_that("quosures are squashed when creating default label for a mapping", {
9898
p <- ggplot(mtcars) + aes(!!quo(identity(!!quo(cyl))))
99-
labels <- ggplot_build(p)$plot@labels
99+
labels <- ggplot_build(p)@plot@labels
100100
expect_identical(labels$x, "identity(cyl)")
101101
})
102102

103103
test_that("labelling doesn't cause error if aesthetic is NULL", {
104104
p <- ggplot(mtcars) + aes(x = NULL)
105-
labels <- ggplot_build(p)$plot@labels
105+
labels <- ggplot_build(p)@plot@labels
106106
expect_identical(labels$x, "x")
107107
})
108108

tests/testthat/test-build.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
df <- data_frame(x = 1:3, y = 3:1, z = letters[1:3])
33

44
test_that("there is one data frame for each layer", {
5-
nlayers <- function(x) length(ggplot_build(x)$data)
5+
nlayers <- function(x) length(ggplot_build(x)@data)
66

77
l1 <- ggplot(df, aes(x, y)) + geom_point()
88
l2 <- ggplot(df, aes(x, y)) + geom_point() + geom_line()

0 commit comments

Comments
 (0)