-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
100 lines (89 loc) · 3.27 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#' Output Valid NBA or WNBA Team Abbreviations
#'
#' @description The abbreviations used in this function are extracted from ESPN
#'
#' @param league One of `"NBA"` or `"WNBA"`
#' @export
#' @return A vector of type `"character"`.
#' @examples
#' # List valid NBA team abbreviations
#' valid_team_names("NBA")
#'
#' # List valid WNBA team abbreviations
#' valid_team_names("WNBA")
valid_team_names <- function(league = c("NBA", "WNBA")){
league <- rlang::arg_match0(league, c("NBA", "WNBA"))
map <- switch (league,
"NBA" = nbaplotR::nba_team_abbr_mapping,
"WNBA" = nbaplotR::wnba_team_abbr_mapping
)
n <- sort(unique(map))
n
}
# Extracted from nflreadr and slightly modified
#' Standardize NBA/WNBA Team Abbreviations
#'
#' This function standardizes NBA/WNBA team abbreviations to ESPN defaults.
#'
#' @param abbr a character vector of abbreviations
#' @param league One of `"NBA"` or `"WNBA"`
#' @param keep_non_matches If `TRUE` (the default) an element of `abbr` that can't
#' be matched to any of the internal mapping vectors will be kept as is. Otherwise
#' it will be replaced with `NA`.
#'
#' @return A character vector with the length of `abbr` and cleaned team abbreviations
#' if they are included in [`nba_team_abbr_mapping`] or [`wnba_team_abbr_mapping`]
#' (depending on the value of `league`). Non matches may be replaced
#' with `NA` (depending on the value of `keep_non_matches`).
#' @export
#' @examples
#' ## NBA EXAMPLES ##
#' a <- c("ALT", "BKN", "BRK", "BROK", "UTAH", "UTA", "UTAA")
#'
#' # keep non matches
#' nbaplotR::clean_team_abbrs(a)
#'
#' # replace non matches
#' nbaplotR::clean_team_abbrs(a, keep_non_matches = FALSE)
#'
#' ## WNBA EXAMPLES ##
#' b <- c("ALT", "CHI", "DAL", "DALL", "PHX", "SEA")
#'
#' # keep non matches
#' nbaplotR::clean_team_abbrs(b, league = "WNBA")
#'
#' # replace non matches
#' nbaplotR::clean_team_abbrs(b, league = "WNBA", keep_non_matches = FALSE)
clean_team_abbrs <- function(abbr,
league = c("NBA", "WNBA"),
keep_non_matches = TRUE) {
stopifnot(is.character(abbr))
league <- rlang::arg_match0(league, c("NBA", "WNBA"))
m <- switch (league,
"NBA" = nbaplotR::nba_team_abbr_mapping,
"WNBA" = nbaplotR::wnba_team_abbr_mapping
)
a <- unname(m[toupper(abbr)])
if (any(is.na(a)) && getOption("nbaplotR.verbose", default = interactive())) {
map <- switch (league,
"NBA" = "nbaplotR::nba_team_abbr_mapping",
"WNBA" = "nbaplotR::wnba_team_abbr_mapping"
)
cli::cli_warn("Abbreviations not found in {.code {map}}: {utils::head(abbr[is.na(a)], 10)}")
}
if (isTRUE(keep_non_matches)) a <- ifelse(!is.na(a), a, abbr)
a
}
# internal helper that outputs local path to logo files
logo_from_abbr <- function(abbr, league = c("NBA", "WNBA")){
img_vctr <- paste0(league, "/", abbr, ".png")
# This used to call the following system.file line
# but it drops non matches which results in errors
# system.file(img_vctr, package = "nbaplotR")
# Now we use some code from system.file but keep the non matches
packagePath <- find.package("nbaplotR", quiet = TRUE)
img_files <- file.path(packagePath, img_vctr)
present <- file.exists(img_files)
img_files[!present] <- img_vctr[!present]
img_files
}