Skip to content

Commit

Permalink
Better messaging for team abbr or gsis ID mismatches
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaseb committed Nov 15, 2024
1 parent 7ed406d commit 2ca323c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
14 changes: 10 additions & 4 deletions R/build_grobs.R
Original file line number Diff line number Diff line change
@@ -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") {
Expand All @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion R/geom_nfl_headshots.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 3 additions & 1 deletion R/geom_nfl_logos.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 3 additions & 1 deletion R/geom_nfl_wordmarks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
26 changes: 20 additions & 6 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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"
Expand All @@ -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") {
Expand All @@ -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)
Expand Down

0 comments on commit 2ca323c

Please sign in to comment.