Skip to content

Commit

Permalink
Merge pull request #423 from nflverse/utils-performance
Browse files Browse the repository at this point in the history
Utils performance & raw pbp functions
  • Loading branch information
mrcaseb authored Sep 3, 2023
2 parents 76e100c + ab7ab10 commit ce2ca0f
Show file tree
Hide file tree
Showing 15 changed files with 913 additions and 700 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: nflfastR
Title: Functions to Efficiently Access NFL Play by Play Data
Version: 4.5.1.9008
Version: 4.5.1.9009
Authors@R:
c(person(given = "Sebastian",
family = "Carl",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export(fast_scraper_roster)
export(fast_scraper_schedules)
export(load_pbp)
export(load_player_stats)
export(missing_raw_pbp)
export(nflverse_sitrep)
export(report)
export(save_raw_pbp)
export(update_db)
import(dplyr)
import(fastrmodels)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- `fixed_drive` now correctly increments on plays where posteam lost a fumble but remains posteam because defteam also lost a fumble during the same play. (#419)
- nflfastR now fixes missing drive number counts in raw pbp data in order to provide accurate drive information. (#420)
- nflfastR now returns correct `kick_distance` on all punts and kickoffs. (#422)
- nflfastR now fully supports loading raw pbp data from local file system. The best way to use this feature is to set `options("nflfastR.raw_directory" = {"your/local/directory"})`. Alternatively, both `build_nflfastR_pbp()` and `fast_scraper()` support the argument `dir` which defaults to the above option. (#423)
- Added the new function `save_raw_pbp()` which efficiently downloads raw play-by-play data and saves it to the local file system. This serves as a helper to setup the system for faster play-by-play parsing via the above functionality. (#423)
- Added the new function `missing_raw_pbp()` that computes a vector of game IDs missing in the local raw play-by-play directory. (#423)


# nflfastR 4.5.1
Expand Down
13 changes: 5 additions & 8 deletions R/build_nflfastR_pbp.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,27 @@
#' }
#' }
build_nflfastR_pbp <- function(game_ids,
dir = getOption("nflfastR.raw_directory", default = NULL),
...,
decode = TRUE,
rules = TRUE) {

if (!is.vector(game_ids) && is.data.frame(game_ids)) game_ids <- game_ids$game_id

if (!is.vector(game_ids)) cli::cli_abort("Param {.code game_ids} is not a valid vector!")
if (!is.vector(game_ids)) cli::cli_abort("Param {.arg game_ids} is not a valid vector!")

if (isTRUE(decode) && !is_installed("gsisdecoder")) {
cli::cli_abort("Package {.val gsisdecoder} required for decoding. Please install it with {.code install.packages(\"gsisdecoder\")}.")
cli::cli_abort("Package {.pkg gsisdecoder} required for decoding. Please install it with {.code install.packages(\"gsisdecoder\")}.")
}

if (isTRUE(rules)) rule_header("Build nflfastR Play-by-Play Data")

game_count <- ifelse(is.vector(game_ids), length(game_ids), nrow(game_ids))
builder <- TRUE

if (game_count > 1) {
cli::cli_ul("{my_time()} | Start download of {game_count} games...")
} else {
cli::cli_ul("{my_time()} | Start download of {game_count} game...")
}
cli::cli_ul("{my_time()} | Start download of {game_count} game{?s}...")

ret <- fast_scraper(game_ids = game_ids, ..., in_builder = builder) %>%
ret <- fast_scraper(game_ids = game_ids, dir = dir, ..., in_builder = builder) %>%
clean_pbp(in_builder = builder) %>%
add_qb_epa(in_builder = builder) %>%
add_xyac(in_builder = builder) %>%
Expand Down
Loading

0 comments on commit ce2ca0f

Please sign in to comment.