Skip to content

Commit

Permalink
Merge pull request #402 from nflverse/sum-play-stats-performance
Browse files Browse the repository at this point in the history
boost sum_play_stats
  • Loading branch information
mrcaseb authored Dec 20, 2022
2 parents 655d11d + c8cf801 commit 12d3ef8
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 15 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.0.9002
Version: 4.5.0.9003
Authors@R:
c(person(given = "Sebastian",
family = "Carl",
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* New implementation of tests to be able to identify breaking changes in reverse dependencies
* `calculate_standings()` no more freezes when computing standings from schedules where some games are missing results, i.e. upcoming games. (v4.5.0.9000)
* Bug fix that caused problems with upcoming dplyr and tidyselect updates that weren't reverse compatible.
* Significant performance improvements of internal functions. (#402)
* Wrap examples in `try()` to avoid CRAN problems. (#404)
* Fixed a bug where `calculate_standings()` wasn't able to handle nflverse pbp data. (#404)

Expand Down
8 changes: 2 additions & 6 deletions R/helper_scrape_gc.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,8 @@ get_pbp_gc <- function(gameId, dir = NULL, qs = FALSE, ...) {
playStatSeq = "sequence"
)


pbp_stats <- furrr::future_map(unique(stats$playId), function(x, s) {
sum_play_stats(x, s)
}, stats)

pbp_stats <- dplyr::bind_rows(pbp_stats)
pbp_stats <- lapply(unique(stats$playId), sum_play_stats, stats)
pbp_stats <- data.table::rbindlist(pbp_stats) %>% tibble::as_tibble()

# drive info
d <- tibble::tibble(drives) %>%
Expand Down
8 changes: 2 additions & 6 deletions R/helper_scrape_nfl.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,9 @@ get_pbp_nfl <- function(id, dir = NULL, qs = FALSE, ...) {

# if I don't put this here it breaks
suppressWarnings(
pbp_stats <-
furrr::future_map(unique(stats$playId), function(x, s) {
sum_play_stats(x, s)
}, stats)
pbp_stats <- lapply(unique(stats$playId), sum_play_stats, stats)
)

pbp_stats <- dplyr::bind_rows(pbp_stats)
pbp_stats <- data.table::rbindlist(pbp_stats) %>% tibble::as_tibble()

combined <- game_info %>%
dplyr::bind_cols(plays %>% dplyr::select(-"playStats", -"game_id")) %>%
Expand Down
4 changes: 2 additions & 2 deletions R/helper_tidy_play_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
# @param stats A dataframe including multiple rows for each play_Id holding
# gsis stat ids and stats
sum_play_stats <- function(play_Id, stats) {
play_stats <- stats %>% filter(.data$playId == play_Id)
play_stats <- stats[stats$playId == play_Id,]

row <- bind_cols(play_id = as.integer(play_Id), tidy_play_stats_row)
row <- c("play_id" = as.integer(play_Id), tidy_play_stats_row)

for (index in seq_along(play_stats$playId)) {
stat_id <- play_stats$statId[index]
Expand Down

0 comments on commit 12d3ef8

Please sign in to comment.