From e331299d15be714aa5b019addb56f319521f6252 Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Sun, 24 Sep 2023 11:34:32 +0200 Subject: [PATCH 1/5] Handle NA gsis IDs in headshot functions --- DESCRIPTION | 2 +- R/build_grobs.R | 1 + R/gt_nfl.R | 7 ++++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cfd236e..6c99e77 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: nflplotR Title: NFL Logo Plots in 'ggplot2' -Version: 1.2.0 +Version: 1.2.0.9001 Authors@R: person("Sebastian", "Carl", , "mrcaseb@gmail.com", role = c("aut", "cre")) Description: A set of functions to visualize National Football League diff --git a/R/build_grobs.R b/R/build_grobs.R index 521b5ab..7e6bddb 100644 --- a/R/build_grobs.R +++ b/R/build_grobs.R @@ -15,6 +15,7 @@ build_grobs <- function(i, alpha, colour, data, type = c("teams", "headshots", " image_to_read <- data$path[i] } 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() diff --git a/R/gt_nfl.R b/R/gt_nfl.R index e89bb2b..ae9da52 100644 --- a/R/gt_nfl.R +++ b/R/gt_nfl.R @@ -191,12 +191,17 @@ gt_nfl_headshots <- function(gt_object, FUN.VALUE = character(1), USE.NAMES = FALSE, FUN = function(id) { + if(is.na(id)) return(NA_character_) ret <- headshot_map$headshot_nfl[headshot_map$gsis_id == id] if(length(ret) == 0) ret <- na_headshot() ret } ) - gt::web_image(image_urls, height = height) + img_tags <- gt::web_image(image_urls, height = height) + # gt::web_image inserts a placeholder for NAs + # We want an empty string instead + img_tags[is.na(image_urls)] <- "" + img_tags } ) } From e2fee6234bccd0e20c5c216a4b57d87110a44953 Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Sun, 24 Sep 2023 11:36:02 +0200 Subject: [PATCH 2/5] News bullet --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 6d805e1..71a6ae2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# nflplotR (development version) + +* The functions `geom_nfl_headshots()` and `gt_nfl_headshots()` better handle `NA` gsis IDs. (#48) + # nflplotR 1.2.0 ## New Features From e4c7bb37ff1e68fb415d2dd7eb5f93c89d870f39 Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Sun, 24 Sep 2023 13:07:55 +0200 Subject: [PATCH 3/5] validate gsis IDs and keep input if it's invalid this is necesary to allow gt::sub_missing --- R/gt_nfl.R | 7 ++++--- R/utils.R | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/R/gt_nfl.R b/R/gt_nfl.R index ae9da52..a76de79 100644 --- a/R/gt_nfl.R +++ b/R/gt_nfl.R @@ -191,7 +191,7 @@ gt_nfl_headshots <- function(gt_object, FUN.VALUE = character(1), USE.NAMES = FALSE, FUN = function(id) { - if(is.na(id)) return(NA_character_) + if(is.na(id) | !is_gsis(id)) return(NA_character_) ret <- headshot_map$headshot_nfl[headshot_map$gsis_id == id] if(length(ret) == 0) ret <- na_headshot() ret @@ -199,8 +199,9 @@ gt_nfl_headshots <- function(gt_object, ) img_tags <- gt::web_image(image_urls, height = height) # gt::web_image inserts a placeholder for NAs - # We want an empty string instead - img_tags[is.na(image_urls)] <- "" + # We want the actual input instead because users might call + # gt::sub_missing which defaults to "---" + img_tags[is.na(image_urls)] <- gsis[is.na(image_urls)] img_tags } ) diff --git a/R/utils.R b/R/utils.R index 12f2e9a..862b188 100644 --- a/R/utils.R +++ b/R/utils.R @@ -45,6 +45,8 @@ headshot_html <- function(player_gsis, type = c("height", "width"), size = 25){ is_installed <- function(pkg) requireNamespace(pkg, quietly = TRUE) +is_gsis <- function(id) grepl("00-00[0-9]{5}", id, perl = TRUE) + load_headshots <- function() nflreadr::rds_from_url("https://github.com/nflverse/nflplotR/releases/download/nflplotr_infrastructure/headshot_gsis_map.rds") na_headshot <- function() "https://static.www.nfl.com/image/private/t_player_profile_landscape_2x/f_auto/league/rfuw3dh4aah4l4eeuubp.png" From 5bbec251a264cfaae3dc32b886a470ed04c32d70 Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Sun, 24 Sep 2023 13:25:00 +0200 Subject: [PATCH 4/5] allow gt::sub_missing for logos and wordmarks --- R/gt_nfl.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/gt_nfl.R b/R/gt_nfl.R index a76de79..ad119dc 100644 --- a/R/gt_nfl.R +++ b/R/gt_nfl.R @@ -95,7 +95,11 @@ gt_nflplotR_image <- function(gt_object, # Create the image URI uri <- get_image_uri(team_abbr = team_abbr, type = type) # Generate the Base64-encoded image and place it within tags - paste0("") + out <- paste0("") + # If the image uri returns NA we didn't find a match. We will return the + # actual value then to allow the user to call gt::sub_missing() + out[is.na(uri)] <- x[is.na(uri)] + out } ) @@ -115,6 +119,8 @@ get_image_uri <- function(team_abbr, type = c("logos", "wordmarks")) { FUN.VALUE = character(1), USE.NAMES = FALSE, FUN = function(team) { + # every non match will return NULL which is when we want NA + if (is.null(lookup_list[[team]])) return(NA_character_) paste0( "data:", "image/png", ";base64,", base64enc::base64encode(lookup_list[[team]]) From dca0b1f4b346ee2082a18c6b55c2e9a1a594849e Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Sun, 24 Sep 2023 13:27:12 +0200 Subject: [PATCH 5/5] one more news bullet --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 71a6ae2..12a0cd9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # nflplotR (development version) * The functions `geom_nfl_headshots()` and `gt_nfl_headshots()` better handle `NA` gsis IDs. (#48) +* The functions `gt_nfl_logos()` and `gt_nfl_wordmarks()` now keep non team name matches to allow the user to call `gt::sub_missing()`. (#48) # nflplotR 1.2.0