Skip to content

Commit 7121e92

Browse files
committed
Incorporate latest ggplot2 class renaming
in tidyverse/ggplot2#6546
1 parent 95f3ecd commit 7121e92

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export(valid_team_names)
3030
import(data.table)
3131
import(grid)
3232
importFrom(ggpath,element_path)
33-
importFrom(ggplot2,draw_element)
3433
importFrom(ggplot2,element)
34+
importFrom(ggplot2,element_grob)
3535
importFrom(graphics,par)
3636
importFrom(lifecycle,deprecated)
3737
importFrom(nflreadr,nflverse_sitrep)

R/nflplotR-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
## usethis namespace: start
77
#' @import data.table
88
#' @import grid
9-
#' @importFrom ggplot2 draw_element element
9+
#' @importFrom ggplot2 element_grob element
1010
#' @importFrom graphics par
1111
#' @importFrom lifecycle deprecated
1212
#' @importFrom rlang .data %||%

R/theme-elements.R

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ element_nfl_headshot <- S7::new_class(
147147
#' @importFrom ggpath element_path
148148
ggpath::element_path
149149

150-
S7::method(draw_element, element_nfl_logo) <- function(
150+
S7::method(element_grob, element_nfl_logo) <- function(
151151
element,
152152
label = "",
153153
x = NULL,
@@ -171,22 +171,47 @@ S7::method(draw_element, element_nfl_logo) <- function(
171171
)
172172
}
173173

174-
S7::method(draw_element, ggpath::element_path)(
174+
# This will likely break in a future ggplot2 release as they
175+
# currently work with a weird mix of S3 and S7. element_grob is a S3 generic
176+
# but the methods are S7. However, I can't call the method from ggpath with
177+
# S7::method(element_grob, ggpath::element_path)(...)
178+
# I think this should work and probably will work at some point in the future
179+
# in the meantime I change the class of element to dispatch to the ggpath
180+
# method for further processing.
181+
class(element) <- c(
182+
"ggpath::element_path",
183+
"ggplot2::element_text",
184+
"ggplot2::element",
185+
"S7_object"
186+
)
187+
188+
ggplot2::element_grob(
175189
element = element,
176190
label = image_list,
177191
x = x,
178192
y = y
179193
)
180194
}
181195

182-
S7::method(draw_element, element_nfl_wordmark) <- function(
196+
S7::method(element_grob, element_nfl_wordmark) <- function(
183197
element,
184198
label = "",
185199
x = NULL,
186200
y = NULL,
187201
...
188202
) {
189-
S7::method(draw_element, element_nfl_logo)(
203+
# see comment on weird S3/S7 mix in the above method
204+
# Note that I dispatch to the nflplotR method here before we move on
205+
# to ggpath
206+
class(element) <- c(
207+
"nflplotR::element_nfl_logo",
208+
"ggpath::element_path",
209+
"ggplot2::element_text",
210+
"ggplot2::element",
211+
"S7_object"
212+
)
213+
214+
ggplot2::element_grob(
190215
element = element,
191216
label = label,
192217
x = x,
@@ -196,7 +221,7 @@ S7::method(draw_element, element_nfl_wordmark) <- function(
196221
)
197222
}
198223

199-
S7::method(draw_element, element_nfl_headshot) <- function(
224+
S7::method(element_grob, element_nfl_headshot) <- function(
200225
element,
201226
label = "",
202227
x = NULL,
@@ -217,7 +242,15 @@ S7::method(draw_element, element_nfl_headshot) <- function(
217242
image_urls[is.na(image_urls)] <- na_headshot()
218243
}
219244

220-
S7::method(draw_element, ggpath::element_path)(
245+
# see comment on weird S3/S7 mix in the above method
246+
class(element) <- c(
247+
"ggpath::element_path",
248+
"ggplot2::element_text",
249+
"ggplot2::element",
250+
"S7_object"
251+
)
252+
253+
ggplot2::element_grob(
221254
element = element,
222255
label = image_urls,
223256
x = x,

tests/testthat/_snaps/theme-elements.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# logo element works
22

33
Code
4-
out <- ggplotGrob(p3)
5-
Message
6-
! Can't find team abbreviation "LARRR". Will insert empty graphic object.
7-
! Can't find team abbreviation "LARRR". Will insert empty graphic object.
4+
out <- ggplot2::ggplot_build(p3)
85

96
---
107

tests/testthat/test-theme-elements.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test_that("logo element works", {
3535
theme(
3636
strip.text = element_nfl_wordmark()
3737
)
38-
expect_snapshot(out <- ggplotGrob(p3))
38+
expect_snapshot(out <- ggplot2::ggplot_build(p3))
3939

4040
# GSIS ID mismatch
4141
p4 <- data.frame(a = c("00-0033077", "00-0012345"), b = 1:2, c = 10:11) |>

0 commit comments

Comments
 (0)