Skip to content

Commit

Permalink
add stats to update workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaseb committed Nov 13, 2024
1 parent 29107f1 commit 27a5b49
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/update_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
fail-fast: false
matrix:
season: ${{ fromJson(needs.pbp_setup.outputs.seasons) }}
type: ["ps_off", "ps_def", "ps_kick"]
type: ["ps_off", "ps_def", "ps_kick", "stats"]
env:
GH_TOKEN: ${{ secrets.NFLVERSE_GH_TOKEN }}
GITHUB_PAT: ${{ secrets.NFLVERSE_GH_TOKEN }}
Expand All @@ -112,7 +112,6 @@ jobs:
extra-packages: |
nflverse/nflverse-data
nflverse/fastrmodels
nflverse/ngsscrapR
- name: Release ${{ matrix.type }}
run: Rscript -e 'source("R/update_data.R")'

Expand Down
6 changes: 4 additions & 2 deletions R/update_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ season <- Sys.getenv("NFLVERSE_UPDATE_SEASON", unset = NA_character_) |> as.inte
type <- Sys.getenv("NFLVERSE_UPDATE_TYPE", unset = NA_character_)
type <- rlang::arg_match0(
type,
c("pbp", "pbp_stats", "ps_off", "ps_def", "ps_kick", "laterals",
c("pbp", "pbp_stats", "ps_off", "ps_def", "ps_kick", "stats", "laterals",
"participation", "ps_off_comb", "ps_def_comb", "ps_kick_comb")
)

Expand All @@ -12,6 +12,7 @@ options(nflreadr.verbose = FALSE)
source("R/update_pbp.R")
source("R/update_playstats.R")
source("R/update_player_stats.R")
source("R/update_stats.R")
source("R/update_multiple_laterals.R")
source("R/update_pbp_participation.R")

Expand All @@ -26,6 +27,7 @@ release <- switch (
"ps_kick" = release_playerstats_kicking,
"ps_kick_comb" = release_playerstats_kicking_combined,
"laterals" = release_lateral_yards,
"participation" = release_pbp_participation
"participation" = release_pbp_participation,
"stats" = release_stats
)
release(season)
138 changes: 138 additions & 0 deletions R/update_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
release_stats <- function(season){
cli::cli_progress_step("Starting {.fct nflfastR::calculate_stats} for {season}!")

# WEEK LEVEL SUMMARY #########################################################

stats_week_player <- nflfastR::calculate_stats(
season = season,
summary_level = "week",
stat_type = "player"
)
attr(stats_week_player, "nflfastR_version") <- as.character(packageVersion("nflfastR"))

stats_week_team <- nflfastR::calculate_stats(
season = season,
summary_level = "week",
stat_type = "team"
)
attr(stats_week_team, "nflfastR_version") <- as.character(packageVersion("nflfastR"))

nflversedata::nflverse_save(
data_frame = stats_week_player,
file_name = glue::glue("stats_player_week_{season}"),
nflverse_type = "player stats: week level",
release_tag = "player_stats",
file_types = c("rds", "csv", "parquet", "qs", "csv.gz")
)

nflversedata::nflverse_save(
data_frame = stats_week_team,
file_name = glue::glue("stats_team_week_{season}"),
nflverse_type = "team stats: week level",
release_tag = "player_stats",
file_types = c("rds", "csv", "parquet", "qs", "csv.gz")
)

# PLAYER SEASON LEVEL SUMMARY ################################################

stats_reg_player <- nflfastR::calculate_stats(
season = season,
summary_level = "season",
stat_type = "player",
season_type = "REG"
)
attr(stats_reg_player, "nflfastR_version") <- as.character(packageVersion("nflfastR"))

stats_post_player <- nflfastR::calculate_stats(
season = season,
summary_level = "season",
stat_type = "player",
season_type = "POST"
)
attr(stats_post_player, "nflfastR_version") <- as.character(packageVersion("nflfastR"))

stats_regpost_player <- nflfastR::calculate_stats(
season = season,
summary_level = "season",
stat_type = "player",
season_type = "REG+POST"
)
attr(stats_regpost_player, "nflfastR_version") <- as.character(packageVersion("nflfastR"))

nflversedata::nflverse_save(
data_frame = stats_reg_player,
file_name = glue::glue("stats_player_reg_{season}"),
nflverse_type = "player stats: season level",
release_tag = "player_stats",
file_types = c("rds", "csv", "parquet", "qs", "csv.gz")
)

nflversedata::nflverse_save(
data_frame = stats_post_player,
file_name = glue::glue("stats_player_post_{season}"),
nflverse_type = "team stats: season level",
release_tag = "player_stats",
file_types = c("rds", "csv", "parquet", "qs", "csv.gz")
)

nflversedata::nflverse_save(
data_frame = stats_regpost_player,
file_name = glue::glue("stats_player_regpost_{season}"),
nflverse_type = "team stats: season level",
release_tag = "player_stats",
file_types = c("rds", "csv", "parquet", "qs", "csv.gz")
)

# TEAM SEASON LEVEL SUMMARY ##################################################

stats_reg_team <- nflfastR::calculate_stats(
season = season,
summary_level = "season",
stat_type = "team",
season_type = "REG"
)
attr(stats_reg_team, "nflfastR_version") <- as.character(packageVersion("nflfastR"))

stats_post_team <- nflfastR::calculate_stats(
season = season,
summary_level = "season",
stat_type = "team",
season_type = "POST"
)
attr(stats_post_team, "nflfastR_version") <- as.character(packageVersion("nflfastR"))

stats_regpost_team <- nflfastR::calculate_stats(
season = season,
summary_level = "season",
stat_type = "team",
season_type = "REG+POST"
)
attr(stats_regpost_team, "nflfastR_version") <- as.character(packageVersion("nflfastR"))

nflversedata::nflverse_save(
data_frame = stats_reg_team,
file_name = glue::glue("stats_team_reg_{season}"),
nflverse_type = "player stats: season level",
release_tag = "player_stats",
file_types = c("rds", "csv", "parquet", "qs", "csv.gz")
)

nflversedata::nflverse_save(
data_frame = stats_post_team,
file_name = glue::glue("stats_team_post_{season}"),
nflverse_type = "team stats: season level",
release_tag = "player_stats",
file_types = c("rds", "csv", "parquet", "qs", "csv.gz")
)

nflversedata::nflverse_save(
data_frame = stats_regpost_team,
file_name = glue::glue("stats_team_regpost_{season}"),
nflverse_type = "team stats: season level",
release_tag = "player_stats",
file_types = c("rds", "csv", "parquet", "qs", "csv.gz")
)

cli::cli_progress_done()
invisible(NULL)
}

0 comments on commit 27a5b49

Please sign in to comment.