diff --git a/DESCRIPTION b/DESCRIPTION index 766ac3e..100118a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: nflplotR Title: NFL Logo Plots in 'ggplot2' -Version: 1.2.0.9007 +Version: 1.2.0.9008 Authors@R: person("Sebastian", "Carl", , "mrcaseb@gmail.com", role = c("aut", "cre")) Description: A set of functions to visualize National Football League @@ -40,4 +40,4 @@ Suggests: Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.0 +RoxygenNote: 7.3.1 diff --git a/NEWS.md b/NEWS.md index f9319e9..176c9e4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ * Deprecated the functions `scale_x_nfl`, `scale_y_nfl`, `scale_x_nfl_headshots`, `scale_y_nfl_headshots`, `theme_x_nfl`, `theme_y_nfl`. These function are slow and require a possibly unstable dependency. Please use the far superior `element_nfl_logo()` and friends instead. (#50) * The function `geom_nfl_logos()` now plots the NFL logo, if `team_abbr == "NFL"`. (#51) * Added the new function `gt_nfl_cols_label()` that renders logos and wordmarks in column labels of {gt} tables. (#52) +* The function `gt_nfl_cols_label()` now allows rendering of player headshots in column labels. Thanks Steven Patton @spatto12 for the PR. (#55) # nflplotR 1.2.0 diff --git a/R/gt_nfl.R b/R/gt_nfl.R index b196215..c635d43 100644 --- a/R/gt_nfl.R +++ b/R/gt_nfl.R @@ -13,8 +13,6 @@ #' [gt::cells_column_labels()], and [gt::cells_row_groups()] helper functions #' can be used here. We can enclose several of these calls within a `list()` #' if we wish to make the transformation happen at different locations. -#' @param type One of `"logo"` or `"wordmark"` selecting whether to render -#' a team's logo or wordmark image. #' #' @return An object of class `gt_tbl`. #' @seealso The player headshot rendering function [gt_nfl_headshots()]. @@ -73,13 +71,52 @@ gt_nfl_wordmarks <- function(gt_object, ) } - -#' @rdname gt_nfl_logos +#' Render Logos, Wordmarks, and Headshots in 'gt' Table Column Labels +#' +#' @description Translate NFL team abbreviations into logos and wordmarks or +#' NFL player gsis IDs to player headshots and render these images in +#' column labels of 'gt' tables. +#' +#' @inheritParams gt_nfl_logos +#' @param ... Currently not in use +#' @param type One of `"logo"`, `"wordmark"`, or `"headshot"` selecting whether +#' to render a team's logo or wordmark image, or a player's headshot. +#' +#' @return An object of class `gt_tbl`. +#' @seealso The article that describes how nflplotR works with the {gt} package +#' +#' @seealso The logo and wordmark rendering functions [gt_nfl_logos()] and +#' [gt_nfl_wordmarks()]. +#' @seealso The player headshot rendering function [gt_nfl_headshots()]. #' @export +#' @section Output of below example: +#' \if{html}{\figure{cols_label.png}{options: width=75\%}} +#' @examples +#' \donttest{ +#' library(gt) +#' label_df <- data.frame( +#' "00-0036355" = 1, +#' "00-0033873" = 2, +#' "LAC" = 11, +#' "KC" = 12, +#' check.names = FALSE +#' ) +#' +#' # create gt table and translate player IDs and team abbreviations +#' # into headshots, logos, and wordmarks +#' table <- gt::gt(label_df) %>% +#' nflplotR::gt_nfl_cols_label( +#' columns = gt::starts_with("00"), +#' type = "headshot" +#' ) %>% +#' nflplotR::gt_nfl_cols_label("LAC", type = "wordmark") %>% +#' nflplotR::gt_nfl_cols_label("KC", type = "logo") +#' } gt_nfl_cols_label <- function(gt_object, columns = gt::everything(), + ..., height = 30, - type = c("logo", "wordmark")){ + type = c("logo", "wordmark", "headshot")){ type <- rlang::arg_match(type) @@ -91,14 +128,23 @@ gt_nfl_cols_label <- function(gt_object, data = gt_object, columns = {{ columns }}, fn = function(x){ - team_abbr <- nflreadr::clean_team_abbrs(as.character(x), keep_non_matches = FALSE) - # Create the image URI - uri <- get_image_uri(team_abbr = team_abbr, type = type) - # Generate the Base64-encoded image and place it within tags - out <- paste0("") - # If the image uri returns NA we didn't find a match. We will return the - # actual value then to avoid removing a label - out[is.na(uri)] <- x[is.na(uri)] + if (type == "headshot"){ + headshots <- load_headshots() + lookup <- headshots$headshot_nfl + names(lookup) <- headshots$gsis_id + image_url <- lookup[x] + out <- gt::web_image(image_url, height = height) + out[is.na(image_url)] <- x[is.na(image_url)] + } else { + team_abbr <- nflreadr::clean_team_abbrs(as.character(x), keep_non_matches = FALSE) + # Create the image URI + uri <- get_image_uri(team_abbr = team_abbr, type = type) + # Generate the Base64-encoded image and place it within tags + out <- paste0("") + # If the image uri returns NA we didn't find a match. We will return the + # actual value then to avoid removing a label + out[is.na(uri)] <- x[is.na(uri)] + } gt::html(out) } ) diff --git a/man/figures/cols_label.png b/man/figures/cols_label.png new file mode 100644 index 0000000..4fef28d Binary files /dev/null and b/man/figures/cols_label.png differ diff --git a/man/gt_nfl_cols_label.Rd b/man/gt_nfl_cols_label.Rd new file mode 100644 index 0000000..979eb29 --- /dev/null +++ b/man/gt_nfl_cols_label.Rd @@ -0,0 +1,71 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/gt_nfl.R +\name{gt_nfl_cols_label} +\alias{gt_nfl_cols_label} +\title{Render Logos, Wordmarks, and Headshots in 'gt' Table Column Labels} +\usage{ +gt_nfl_cols_label( + gt_object, + columns = gt::everything(), + ..., + height = 30, + type = c("logo", "wordmark", "headshot") +) +} +\arguments{ +\item{gt_object}{A table object that is created using the \code{\link[gt:gt]{gt::gt()}} function.} + +\item{columns}{The columns for which the image translation should be applied. +Argument has no effect if \code{locations} is not \code{NULL}.} + +\item{...}{Currently not in use} + +\item{height}{The absolute height (px) of the image in the table cell.} + +\item{type}{One of \code{"logo"}, \code{"wordmark"}, or \code{"headshot"} selecting whether +to render a team's logo or wordmark image, or a player's headshot.} +} +\value{ +An object of class \code{gt_tbl}. +} +\description{ +Translate NFL team abbreviations into logos and wordmarks or +NFL player gsis IDs to player headshots and render these images in +column labels of 'gt' tables. +} +\section{Output of below example}{ + +\if{html}{\figure{cols_label.png}{options: width=75\%}} +} + +\examples{ +\donttest{ +library(gt) +label_df <- data.frame( + "00-0036355" = 1, + "00-0033873" = 2, + "LAC" = 11, + "KC" = 12, + check.names = FALSE +) + +# create gt table and translate player IDs and team abbreviations +# into headshots, logos, and wordmarks +table <- gt::gt(label_df) \%>\% + nflplotR::gt_nfl_cols_label( + columns = gt::starts_with("00"), + type = "headshot" + ) \%>\% + nflplotR::gt_nfl_cols_label("LAC", type = "wordmark") \%>\% + nflplotR::gt_nfl_cols_label("KC", type = "logo") +} +} +\seealso{ +The article that describes how nflplotR works with the {gt} package +\url{https://nflplotr.nflverse.com/articles/gt.html} + +The logo and wordmark rendering functions \code{\link[=gt_nfl_logos]{gt_nfl_logos()}} and +\code{\link[=gt_nfl_wordmarks]{gt_nfl_wordmarks()}}. + +The player headshot rendering function \code{\link[=gt_nfl_headshots]{gt_nfl_headshots()}}. +} diff --git a/man/gt_nfl_logos.Rd b/man/gt_nfl_logos.Rd index 4c3a18d..49332a6 100644 --- a/man/gt_nfl_logos.Rd +++ b/man/gt_nfl_logos.Rd @@ -3,19 +3,11 @@ \name{gt_nfl_logos} \alias{gt_nfl_logos} \alias{gt_nfl_wordmarks} -\alias{gt_nfl_cols_label} \title{Render Logos and Wordmarks in 'gt' Tables} \usage{ gt_nfl_logos(gt_object, columns, height = 30, locations = NULL) gt_nfl_wordmarks(gt_object, columns, height = 30, locations = NULL) - -gt_nfl_cols_label( - gt_object, - columns = gt::everything(), - height = 30, - type = c("logo", "wordmark") -) } \arguments{ \item{gt_object}{A table object that is created using the \code{\link[gt:gt]{gt::gt()}} function.} @@ -32,9 +24,6 @@ transformation. Only the \code{\link[gt:cells_body]{gt::cells_body()}}, \code{\l \code{\link[gt:cells_column_labels]{gt::cells_column_labels()}}, and \code{\link[gt:cells_row_groups]{gt::cells_row_groups()}} helper functions can be used here. We can enclose several of these calls within a \code{list()} if we wish to make the transformation happen at different locations.} - -\item{type}{One of \code{"logo"} or \code{"wordmark"} selecting whether to render -a team's logo or wordmark image.} } \value{ An object of class \code{gt_tbl}. diff --git a/vignettes/articles/gt.Rmd b/vignettes/articles/gt.Rmd index 25db7b7..c8d281f 100644 --- a/vignettes/articles/gt.Rmd +++ b/vignettes/articles/gt.Rmd @@ -154,6 +154,22 @@ gt::gt(df) |> ) ``` +HEADSHOTS: + +```{r} +headshot_df <- data.frame( + "00-0036355" = 1, + "00-0033873" = 2, + check.names = FALSE +) +gt::gt(headshot_df) |> + nflplotR::gt_nfl_cols_label(type = "headshot") |> + # align the complete table left + gt::tab_options( + table.align = "left" + ) +``` + ### Logos and Wordmarks Rendered by nflplotR This example creates a table that renders all team logos and wordmarks. We split the table into 2 x 16 rows to avoid an overly long table and convert all variables starting with "logo" to logos and all variables starting with "wordmark" to wordmarks.