Skip to content

Commit

Permalink
Improve abbreviation coverage
Browse files Browse the repository at this point in the history
Close #29
  • Loading branch information
k5cents committed Oct 6, 2021
1 parent 9ca0744 commit 345f722
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 34 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fflr
Title: Retrieve ESPN Fantasy Football Data
Version: 1.9.2.9001
Version: 1.9.2.9002
Authors@R:
person(given = "Kiernan",
family = "Nicholls",
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# fflr (development version)

* Improve abbreviation techniques, add all roster slot and position IDs (#29).
* Fix error checking in `ffl_api()`. Returns proper error message when there is
a failure (e.g., non-public league) (#36).

Expand Down
49 changes: 49 additions & 0 deletions R/abbrev.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# position abbrev slot pos
# Quarterback QB 0 1
# Team Quarterback TQB 1
# Running Back RB 2 2
# Running Back/Wide Receiver RB/WR 3
# Wide Receiver WR 4 3
# Wide Receiver/Tight End WR/TE 5
# Tight End TE 6 4
# Flex FLEX 23
# Offensive Player Utility OP 7
# Defensive Tackle DT 8 9
# Defensive End DE 9 10
# Linebacker LB 10 11
# Defensive Line DL 11
# Cornerback CB 12 12
# Safety S 13 13
# Defensive Back DB 14
# Defensive Player Utility DP 15
# Team Defense/Special Teams D/ST 16 16
# Place Kicker K 17 5
# Punter P 18 7
# Head Coach HC 19 14
# Bench BE 20
# Injured Reserve IR 21

slot_abbrev <- function(slot) {
stopifnot(is.numeric(slot))
factor(slot, levels = pos_ids$slot, labels = pos_ids$abbrev)
}

slot_unabbrev <- function(abbrev) {
stopifnot(is.character(abbrev))
pos_ids$slot[match(abbrev, pos_ids$abbrev)]
}

pos_abbrev <- function(pos) {
pos_abbrev <- pos_ids$abbrev[!is.na(pos_ids$position)]
stopifnot(is.numeric(pos))
factor(pos, levels = pos_ids$position, labels = pos_abbrev)
}

pos_unabbrev <- function(abbrev) {
stopifnot(is.character(abbrev))
pos_ids$position[match(abbrev, pos_ids$abbrev)]
}

pro_abbrev <- function(proTeamId) {
fflr::nfl_teams$abbrev[match(proTeamId, fflr::nfl_teams$proTeamId)]
}
2 changes: 1 addition & 1 deletion R/roster.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ out_roster <- function(entry, t = NULL) {
id = player$id,
firstName = player$firstName,
lastName = player$lastName,
proTeam = nfl_abb$abbrev[match(player$proTeamId, nfl_abb$id)],
proTeam = pro_abbrev(player$proTeamId),
position = pos_abbrev(player$defaultPositionId),
injuryStatus = injury_status,
projectedScore = proj_dbl,
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
32 changes: 0 additions & 32 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,6 @@ is_installed <- function(pkg) {
isTRUE(requireNamespace(pkg, quietly = TRUE))
}

slot_abbrev <- function(slot) {
factor(
x = slot,
levels = c("0", "2", "4", "6", "23", "16", "17", "20", "21"),
labels = c("QB", "RB", "WR", "TE", "FX", "DS", "PK", "BE", "IR")
)
}

slot_unabbrev <- function(abbrev) {
slot_int <- c(0, 2, 4, 6, 23, 16, 17, 20, 21)
slot_abb <- c("QB", "RB", "WR", "TE", "FX", "DS", "PK", "BE", "IR")
slot_int[match(abbrev, slot_abb)]
}

pos_abbrev <- function(pos) {
factor(
x = pos,
levels = c("1", "2", "3", "4", "5", "9", "16"),
labels = c("QB", "RB", "WR", "TE", "PK", "DT", "DS")
)
}

pos_unabbrev <- function(abbrev) {
pos_int <- c(1, 2, 3, 4, 5, 9, 16)
pos_abb <- c("QB", "RB", "WR", "TE", "PK", "DT", "DS")
pos_int[match(abbrev, pos_abb)]
}

pro_abbrev <- function(proTeamId) {
fflr::nfl_teams$abbrev[match(proTeamId, fflr::nfl_teams$proTeamId)]
}

list_ifnot <- function(x) {
if (!is.list(x)) {
list(x)
Expand Down
57 changes: 57 additions & 0 deletions data-raw/position_ids.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## code to prepare `position_ids` dataset goes here

# Quarterback QB 0 1
# Team Quarterback TQB 1
# Running Back RB 2 2
# Running Back/Wide Receiver RB/WR 3
# Wide Receiver WR 4 3
# Wide Receiver/Tight End WR/TE 5
# Tight End TE 6 4
# Flex FLEX 23
# Offensive Player Utility OP 7
# Defensive Tackle DT 8 9
# Defensive End DE 9 10
# Linebacker LB 10 11
# Defensive Line DL 11
# Cornerback CB 12 12
# Safety S 13 13
# Defensive Back DB 14
# Defensive Player Utility DP 15
# Team Defense/Special Teams D/ST 16 16
# Place Kicker K 17 5
# Punter P 18 7
# Head Coach HC 19 14
# Bench BE 20
# Injured Reserve IR 21

pos_ids <- tibble::tribble(
~name, ~abbrev, ~slot, ~position,
"Quarterback", "QB", 0, 1,
"Team Quarterback", "TQB", 1, NA,
"Running Back", "RB", 2, 2,
"Running Back/Wide Receiver", "RB/WR", 3, NA,
"Wide Receiver", "WR", 4, 3,
"Wide Receiver/Tight End", "WR/TE", 5, NA,
"Tight End", "TE", 6, 4,
"Flex", "FLEX", 23, NA,
"Offensive Player Utility", "OP", 7, NA,
"Defensive Tackle", "DT", 8, 9,
"Defensive End", "DE", 9, 10,
"Linebacker", "LB", 10, 11,
"Defensive Line", "DL", 11, NA,
"Cornerback", "CB", 12, 12,
"Safety", "S", 13, 13,
"Defensive Back", "DB", 14, NA,
"Defensive Player Utility", "DP", 15, NA,
"Team Defense/Special Teams", "D/ST", 16, 16,
"Place Kicker", "K", 17, 5,
"Punter", "P", 18, 7,
"Head Coach", "HC", 19, 14,
"Bench", "BE", 20, NA ,
"Injured Reserve", "IR", 21, NA
)

pos_ids$slot <- as.integer(pos_ids$slot)
pos_ids$position <- as.integer(pos_ids$position)

usethis::use_data(pos_ids, internal = TRUE, overwrite = TRUE)

0 comments on commit 345f722

Please # to comment.