diff --git a/R/build_grobs.R b/R/build_grobs.R index f80403d..8726f44 100644 --- a/R/build_grobs.R +++ b/R/build_grobs.R @@ -1,6 +1,6 @@ # INTERNAL HELPER THAT BUILDS THE GROBS FOR # GEOM LOGOS, WORDMARKS AND HEADSHOTS -build_grobs <- function(i, alpha, colour, data, type = c("teams", "headshots", "wordmarks")) { +build_grobs <- function(i, alpha, colour, data, type = c("teams", "headshots", "wordmarks"), headshot_map = NULL) { make_null <- FALSE type <- rlang::arg_match(type) if(type == "teams") { @@ -13,12 +13,18 @@ build_grobs <- function(i, alpha, colour, data, type = c("teams", "headshots", " if (is.na(team_abbr) | is.null(image_to_read)) make_null <- TRUE } else { gsis <- data$player_gsis[i] - if (is.na(gsis)) make_null <- TRUE - headshot_map <- load_headshots() image_to_read <- headshot_map$headshot_nfl[headshot_map$gsis_id == gsis] - if(length(image_to_read) == 0) image_to_read <- na_headshot() + if(length(image_to_read) == 0 | all(is.na(image_to_read))){ + cli::cli_alert_warning( + "No headshot available for gsis ID {.val {data$player_gsis[i]}}. Will insert placeholder." + ) + image_to_read <- na_headshot() + } } if (isTRUE(make_null)){ + cli::cli_alert_warning( + "Can't find team abbreviation {.val {data$team_abbr[i]}}. Will insert empty grob." + ) grid <- grid::nullGrob() } else if (is.null(alpha)) { img <- reader_function(image_to_read) diff --git a/R/geom_nfl_headshots.R b/R/geom_nfl_headshots.R index 6f515ca..9edc6cb 100644 --- a/R/geom_nfl_headshots.R +++ b/R/geom_nfl_headshots.R @@ -123,8 +123,17 @@ GeomNFLheads <- ggplot2::ggproto( ), draw_panel = function(data, panel_params, coord, na.rm = FALSE) { data <- coord$transform(data, panel_params) + headshots <- load_headshots() - grobs <- lapply(seq_along(data$player_gsis), build_grobs, alpha = data$alpha, colour = data$colour, data = data, type = "headshots") + grobs <- lapply( + seq_along(data$player_gsis), + build_grobs, + alpha = data$alpha, + colour = data$colour, + data = data, + type = "headshots", + headshot_map = headshots + ) class(grobs) <- "gList" diff --git a/R/geom_nfl_logos.R b/R/geom_nfl_logos.R index 0c8b190..253b96f 100644 --- a/R/geom_nfl_logos.R +++ b/R/geom_nfl_logos.R @@ -129,7 +129,9 @@ GeomNFLlogo <- ggplot2::ggproto( draw_panel = function(data, panel_params, coord, na.rm = FALSE) { data <- coord$transform(data, panel_params) - data$team_abbr <- nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = TRUE) + data$team_abbr <- suppressWarnings( + nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = TRUE) + ) grobs <- lapply(seq_along(data$team_abbr), build_grobs, alpha = data$alpha, colour = data$colour, data = data, type = "teams") diff --git a/R/geom_nfl_wordmarks.R b/R/geom_nfl_wordmarks.R index 411ac1c..1a77be4 100644 --- a/R/geom_nfl_wordmarks.R +++ b/R/geom_nfl_wordmarks.R @@ -123,7 +123,9 @@ GeomNFLwordmark <- ggplot2::ggproto( draw_panel = function(data, panel_params, coord, na.rm = FALSE) { data <- coord$transform(data, panel_params) - data$team_abbr <- nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = FALSE) + data$team_abbr <- suppressWarnings( + nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = TRUE) + ) grobs <- lapply(seq_along(data$team_abbr), build_grobs, alpha = data$alpha, colour = data$colour, data = data, type = "wordmarks") diff --git a/R/theme-elements.R b/R/theme-elements.R index 0509e07..fc11930 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -186,7 +186,9 @@ element_grob.element_nfl_logo <- function(element, label = "", x = NULL, y = NUL alpha <- alpha %||% element$alpha colour <- colour %||% rep(element$colour, n) size <- size %||% element$size - label <- nflreadr::clean_team_abbrs(as.character(label), keep_non_matches = FALSE) + label <- suppressWarnings( + nflreadr::clean_team_abbrs(as.character(label), keep_non_matches = TRUE) + ) grobs <- lapply( seq_along(label), @@ -226,7 +228,9 @@ element_grob.element_nfl_wordmark <- function(element, label = "", x = NULL, y = alpha <- alpha %||% element$alpha colour <- colour %||% rep(element$colour, n) size <- size %||% element$size - label <- nflreadr::clean_team_abbrs(as.character(label), keep_non_matches = FALSE) + label <- suppressWarnings( + nflreadr::clean_team_abbrs(as.character(label), keep_non_matches = TRUE) + ) grobs <- lapply( seq_along(label), @@ -267,6 +271,7 @@ element_grob.element_nfl_headshot <- function(element, label = "", x = NULL, y = alpha <- alpha %||% element$alpha colour <- colour %||% rep(element$colour, n) size <- size %||% element$size + headshots <- load_headshots() grobs <- lapply( seq_along(label), @@ -278,7 +283,8 @@ element_grob.element_nfl_headshot <- function(element, label = "", x = NULL, y = y = y, hjust = hj, vjust = vj, - type = "headshots" + type = "headshots", + headshot_map = headshots ) class(grobs) <- "gList" @@ -293,7 +299,8 @@ element_grob.element_nfl_headshot <- function(element, label = "", x = NULL, y = axisImageGrob <- function(i, label, alpha, colour, x, y, hjust, vjust, width = 1, height = 1, - type = c("teams", "headshots", "wordmarks")) { + type = c("teams", "headshots", "wordmarks"), + headshot_map = NULL) { make_null <- FALSE type <- rlang::arg_match(type) if(type == "teams") { @@ -306,11 +313,18 @@ axisImageGrob <- function(i, label, alpha, colour, x, y, hjust, vjust, if (is.na(team_abbr) | is.null(image_to_read)) make_null <- TRUE } else { gsis <- label[i] - headshot_map <- load_headshots() image_to_read <- headshot_map$headshot_nfl[headshot_map$gsis_id == gsis] - if(length(image_to_read) == 0) image_to_read <- na_headshot() + if(length(image_to_read) == 0 | all(is.na(image_to_read))){ + cli::cli_alert_warning( + "No headshot available for gsis ID {.val {label[i]}}. Will insert placeholder." + ) + image_to_read <- na_headshot() + } } if (isTRUE(make_null)){ + cli::cli_alert_warning( + "Can't find team abbreviation {.val {label[i]}}. Will insert empty grob." + ) return(grid::nullGrob()) } else if (is.null(alpha[i])) { img <- reader_function(image_to_read)