Skip to content

Commit 5a877c3

Browse files
committed
get_layer_data(), get_layer_grob() now accept integers or characters
1 parent bc143c3 commit 5a877c3

File tree

4 files changed

+49
-34
lines changed

4 files changed

+49
-34
lines changed

R/plot-build.R

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
#' layer. These are useful for tests.
1212
#'
1313
#' @param plot ggplot object
14-
#' @param i An integer. In `get_layer_data()`, the data to return (in the order added to the
14+
#' @param i An integer or a name of a layer. In `get_layer_data()`, the data to return (in the order added to the
1515
#' plot). In `get_layer_grob()`, the grob to return (in the order added to the
16-
#' plot). In `get_panel_scales()`, the row of a facet to return scales for.
16+
#' plot). In `get_panel_scales()` (only integers allowed), the row of a facet to return scales for.
1717
#' @param j An integer. In `get_panel_scales()`, the column of a facet to return
1818
#' scales for.
19-
#' @param name A scalar string. In `get_layer_data()` and `get_layer_grob()`, the name of the layer
20-
#' to return. If provided and existing, this takes precedence over `i`.
2119
#' @param ... Not currently in use.
2220
#' @seealso
2321
#' [print.ggplot()] and [benchplot()] for
@@ -143,14 +141,14 @@ build_ggplot <- S7::method(ggplot_build, class_ggplot) <- function(plot, ...) {
143141

144142
#' @export
145143
#' @rdname ggplot_build
146-
get_layer_data <- function(plot = get_last_plot(), i = 1L, name = NA) {
147-
if (is.na(name)) {
148-
idx <- i
149-
} else {
150-
name <- arg_match0(name, names(p@layers))
151-
idx <- which(name == names(p@layers))
152-
}
153-
ggplot_build(plot)@data[[idx]]
144+
get_layer_data <- function(plot = get_last_plot(), i = 1L) {
145+
b <- ggplot_build(plot)
146+
idx <- vec_as_location2(
147+
i = i,
148+
n = vec_size(b@plot@layers),
149+
names = names(b@plot@layers)
150+
)
151+
b@data[[idx]]
154152
}
155153

156154
#' @export
@@ -177,14 +175,14 @@ layer_scales <- get_panel_scales
177175

178176
#' @export
179177
#' @rdname ggplot_build
180-
get_layer_grob <- function(plot = get_last_plot(), i = 1L, name = NA) {
178+
get_layer_grob <- function(plot = get_last_plot(), i = 1L) {
181179
b <- ggplot_build(plot)
182-
if (is.na(name)) {
183-
idx <- i
184-
} else {
185-
idx <- arg_match0(name, names(p@layers))
186-
idx <- which(name == names(p@layers))
187-
}
180+
181+
idx <- vec_as_location2(
182+
i = i,
183+
n = vec_size(b@plot@layers),
184+
names = names(b@plot@layers)
185+
)
188186
b@plot@layers[[idx]]$draw_geom(b@data[[idx]], b@layout)
189187
}
190188

man/ggplot_build.Rd

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/layer.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,23 @@
147147

148148
# get_layer_data works with layer names
149149

150-
`name` must be one of "foo" or "bar", not "none".
150+
Can't extract elements that don't exist.
151+
x Element `none` doesn't exist.
152+
153+
---
154+
155+
Can't extract elements past the end.
156+
i Location 4 doesn't exist.
157+
i There are only 2 elements.
151158

152159
# get_layer_grob works with layer names
153160

154-
`name` must be one of "foo" or "bar", not "none".
161+
Can't extract elements that don't exist.
162+
x Element `none` doesn't exist.
163+
164+
---
165+
166+
Can't extract elements past the end.
167+
i Location 4 doesn't exist.
168+
i There are only 2 elements.
155169

tests/testthat/test-layer.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,16 @@ test_that("get_layer_data works with layer names", {
231231

232232
# name has higher precedence than index
233233
expect_identical(
234-
get_layer_data(p, i = 1L, name = "bar"),
234+
get_layer_data(p, i = "bar"),
235235
get_layer_data(p, i = 2L)
236236
)
237237

238238
# name falls back to index
239239
expect_snapshot_error(
240-
get_layer_data(p, i = 1L, name = "none")
240+
get_layer_data(p, i ="none")
241+
)
242+
expect_snapshot_error(
243+
get_layer_data(p, i = 4L)
241244
)
242245
})
243246

@@ -246,13 +249,16 @@ test_that("get_layer_grob works with layer names", {
246249

247250
# name has higher precedence than index
248251
expect_identical(
249-
get_layer_grob(p, i = 1L, name = "bar"),
252+
get_layer_grob(p, i = "bar"),
250253
get_layer_grob(p, i = 2L)
251254
)
252255

253256
# name falls back to index
254257
expect_snapshot_error(
255-
get_layer_grob(p, i = 1L, name = "none")
258+
get_layer_grob(p, i ="none")
259+
)
260+
expect_snapshot_error(
261+
get_layer_grob(p, i = 4L)
256262
)
257263
})
258264

0 commit comments

Comments
 (0)