Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gt_nfl.R #55

Merged
merged 4 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"))
Description: A set of functions to visualize National Football League
Expand Down Expand Up @@ -40,4 +40,4 @@ Suggests:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
72 changes: 59 additions & 13 deletions R/gt_nfl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()].
Expand Down Expand Up @@ -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
#' <https://nflplotr.nflverse.com/articles/gt.html>
#' @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)

Expand All @@ -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 <img> tags
out <- paste0("<img src=\"", uri, "\" style=\"height:", height, ";\">")
# 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 <img> tags
out <- paste0("<img src=\"", uri, "\" style=\"height:", height, ";\">")
# 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)
}
)
Expand Down
Binary file added man/figures/cols_label.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions man/gt_nfl_cols_label.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions man/gt_nfl_logos.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vignettes/articles/gt.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading