Skip to contents

Translate player IDs to player headshots and render these images in html tables with the 'gt' package. IDs are GSIS IDs for the NFL ("00-0033873", resolved through the headshot map sdvplotR publishes from nflverse rosters to the player's NFL.com headshot) and ESPN athlete IDs for every other sport, or as id_type says. IDs that resolve to no headshot are left as text.

Usage

gt_sdv_headshots(
  gt_object,
  columns,
  sport = c("nfl", "nba", "wnba", "mlb", "nhl", "cfb", "mbb", "wbb"),
  height = 30,
  locations = NULL,
  id_type = NULL
)

Arguments

gt_object

A table object created using gt::gt().

columns

The columns for which the image translation should be applied. Argument has no effect if locations is not NULL.

sport

Character string identifying the sport.

height

The absolute height (px) of the image in the table cell.

locations

If NULL (the default), the function will render logos in argument columns. Otherwise, the cell or set of cells to be associated with the team name transformation. Only gt::cells_body(), gt::cells_stub(), gt::cells_column_labels(), and gt::cells_row_groups() helper functions can be used here.

id_type

Which ID system player_id holds. NULL (the default) takes GSIS IDs for the NFL and ESPN athlete IDs for every other sport. "espn" takes ESPN athlete IDs for any sport, the IDs in ESPN-sourced data such as hoopR's and wehoop's espn_*() functions. "league" takes the league's own ID: the GSIS ID for the NFL, the NBA Stats or WNBA Stats PERSON_ID (hoopR's nba_*(), wehoop's wnba_*()), the MLBAM ID (baseballr's mlb_*(), Baseball Savant) for MLB, or the NHL API player ID (fastRhockey's nhl_*() and load_nhl_*()) for the NHL, drawn from that league's image CDN; college sports have no league option. ESPN and league IDs other than GSIS are plain digits, so a mismatch draws the wrong player or no image rather than an error. League CDNs draw a silhouette for an unknown ID, and the NBA and WNBA CDNs refuse requests from datacenter IPs, so a plot drawn on CI or a server can come back without those headshots.

Value

An object of class gt_tbl.

Examples

# \donttest{
library(gt)
library(sdvplotR)

df <- data.frame(
  player_id = c("00-0033873", "00-0026498", "00-0035228"),
  player_name = c("P.Mahomes", "M.Stafford", "K.Murray")
)

df |>
  gt() |>
  gt_sdv_headshots(columns = "player_id", sport = "nfl")
player_id player_name
P.Mahomes
M.Stafford
K.Murray
# }