From 27a5b49b461876792d9b05c3a1fd39881c44ad0d Mon Sep 17 00:00:00 2001 From: Sebastian Carl Date: Wed, 13 Nov 2024 14:32:54 +0100 Subject: [PATCH] add stats to update workflow --- .github/workflows/update_data.yaml | 3 +- R/update_data.R | 6 +- R/update_stats.R | 138 +++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 R/update_stats.R diff --git a/.github/workflows/update_data.yaml b/.github/workflows/update_data.yaml index 434dada1..50550b10 100644 --- a/.github/workflows/update_data.yaml +++ b/.github/workflows/update_data.yaml @@ -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 }} @@ -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")' diff --git a/R/update_data.R b/R/update_data.R index 51473a0e..d366fe03 100644 --- a/R/update_data.R +++ b/R/update_data.R @@ -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") ) @@ -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") @@ -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) diff --git a/R/update_stats.R b/R/update_stats.R new file mode 100644 index 00000000..f02aa1b4 --- /dev/null +++ b/R/update_stats.R @@ -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) +}