Skip to content

Commit bd302f8

Browse files
committed
support method calls from outside ggpath
1 parent b0575fc commit bd302f8

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

R/build_grobs.R

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22
build_grobs <- function(i, alpha, colour, path, data,
33
is_theme_element = FALSE,
44
call = rlang::caller_env()) {
5-
img <- try(reader_function(path[i]), silent = TRUE)
5+
to_read <- path[i]
6+
7+
# to_read might be a list of length 1 if ggpath's S7 method is called
8+
# from other packages, e.g. nflplotR
9+
# If that is the case, we unlist and check whether the object is NULL.
10+
# If it is NULL we can return a NULL grob silently as the calling packages
11+
# are supposed to alert the user about non matches.
12+
if (is.list(to_read)) {
13+
to_read <- unlist(to_read, recursive = FALSE, use.names = FALSE)
14+
}
15+
if (is.null(to_read)) return(ggpath_null_grob(data = data, i = i))
16+
17+
img <- try(reader_function(to_read), silent = TRUE)
618

719
# if the path is invalid we warn the user and insert a NULL grob
820
if (inherits(img, "try-error")) {
921
cli::cli_warn(
10-
"{.pkg ggpath} failed to read an image from {.path {path[i]}}. \\
11-
It will insert an empty grob instead. Here is the \\
22+
"{.pkg ggpath} failed to read an image from {.path {to_read}}. \\
23+
It will insert an empty graphic object instead. Here is the \\
1224
error message: {img}"
1325
)
1426

15-
return({
16-
grid::nullGrob(
17-
name = paste0("ggpath.grob.", i),
18-
vp = grid::viewport(
19-
x = grid::unit(data$x[i], "native"),
20-
y = grid::unit(data$y[i], "native")
21-
)
22-
)
23-
})
27+
return(ggpath_null_grob(data = data, i = i))
2428
}
2529

2630
if (is.null(alpha) | all(alpha == 1L)) { # no alpha requested
@@ -89,3 +93,13 @@ resolve_img_color <- function(img, col){
8993
}
9094
modified_img
9195
}
96+
97+
ggpath_null_grob <- function(data, i){
98+
grid::nullGrob(
99+
name = paste0("ggpath.grob.", i),
100+
vp = grid::viewport(
101+
x = grid::unit(data$x[i], "native"),
102+
y = grid::unit(data$y[i], "native")
103+
)
104+
)
105+
}

R/theme_element.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ S7::method(draw_element, element_path) <- function(
198198
) {
199199
if (is.null(label)) return(ggplot2::zeroGrob())
200200

201-
n <- max(length(x), length(y), 1)
201+
n <- max(length(x), length(y), length(label), 1)
202202
vj <- element@vjust %||% vjust
203203
hj <- element@hjust %||% hjust
204204
angle <- element@angle %||% angle

0 commit comments

Comments
 (0)