|
2 | 2 | build_grobs <- function(i, alpha, colour, path, data, |
3 | 3 | is_theme_element = FALSE, |
4 | 4 | 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) |
6 | 18 |
|
7 | 19 | # if the path is invalid we warn the user and insert a NULL grob |
8 | 20 | if (inherits(img, "try-error")) { |
9 | 21 | 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 \\ |
12 | 24 | error message: {img}" |
13 | 25 | ) |
14 | 26 |
|
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)) |
24 | 28 | } |
25 | 29 |
|
26 | 30 | if (is.null(alpha) | all(alpha == 1L)) { # no alpha requested |
@@ -89,3 +93,13 @@ resolve_img_color <- function(img, col){ |
89 | 93 | } |
90 | 94 | modified_img |
91 | 95 | } |
| 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 | +} |
0 commit comments