|
| 1 | +#' Fetch Team Statistics |
| 2 | +#' |
| 3 | +#' General wrapper for fetching team statistics from a specified source. |
| 4 | +#' |
| 5 | +#' @param season Integer. The season to fetch stats for (e.g. 2024). |
| 6 | +#' @param summary_type Character. Either `"totals"` (default) or `"averages"`. |
| 7 | +#' @param source Character. Currently only `"afltables"` is supported. |
| 8 | +#' @param ... Additional arguments passed to the underlying data source function. |
| 9 | +#' |
| 10 | +#' @return A data frame of team stats for the season. |
| 11 | +#' @export |
| 12 | +#' |
| 13 | +#' @examples |
| 14 | +#' fetch_team_stats(2023) |
| 15 | +#' fetch_team_stats(2024, summary_type = "averages") |
| 16 | +fetch_team_stats<-function(season, |
| 17 | +summary_type="totals", |
| 18 | +source="afltables", |
| 19 | +...) { |
| 20 | +dat<-switch(source, |
| 21 | +"afltables"= fetch_team_stats_afltables(season,summary_type), |
| 22 | +NULL |
| 23 | + ) |
| 24 | + |
| 25 | +if (is.null(dat)) { |
| 26 | +cli::cli_warn('The source "{source}" is not supported.') |
| 27 | + } |
| 28 | + |
| 29 | +return(dat) |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +#' Fetch Team Statistics from AFLTables |
| 34 | +#' |
| 35 | +#' Scrapes team-level statistics from AFLTables.com for a given season. |
| 36 | +#' |
| 37 | +#' @param season Integer. A season (e.g. 2024). |
| 38 | +#' @param summary_type Character. Either `"totals"` (default) or `"averages"`. |
| 39 | +#' |
| 40 | +#' @return A data frame with team statistics from AFLTables. |
| 41 | +#' @keywords internal |
| 42 | +fetch_team_stats_afltables<-function(season,summary_type="totals") { |
| 43 | +if (!is.numeric(season)||season<1965) { |
| 44 | +cli::cli_abort('Season must be numeric and greater than or equal to 1965. You provided "{season}".') |
| 45 | + } |
| 46 | + |
| 47 | +cli::cli_progress_step("Downloading team stats from AFLTables for {season}") |
| 48 | + |
| 49 | +url<-glue::glue("https://afltables.com/afl/stats/{season}s.html") |
| 50 | +page<- tryCatch(rvest::read_html(url),error=function(e)NULL) |
| 51 | + |
| 52 | +if (is.null(page)) { |
| 53 | +cli::cli_abort("Could not access AFLTables page for season {season}.") |
| 54 | + } |
| 55 | + |
| 56 | +tables<-page|>rvest::html_elements("table") |
| 57 | + |
| 58 | +if (length(tables)<3) { |
| 59 | +cli::cli_abort("Insufficient tables found on the page for season: {season}") |
| 60 | + } |
| 61 | + |
| 62 | +team_totals_for<-tables[[2]]|>rvest::html_table(fill=TRUE) |
| 63 | +team_totals_against<-tables[[3]]|>rvest::html_table(fill=TRUE) |
| 64 | + |
| 65 | + colnames(team_totals_for)[1]<-"Team" |
| 66 | + colnames(team_totals_against)[1]<-"Team" |
| 67 | +team_totals_for$type<-"for" |
| 68 | +team_totals_against$type<-"against" |
| 69 | + |
| 70 | +team_stats<-dplyr::bind_rows(team_totals_for,team_totals_against)|> |
| 71 | +dplyr::filter(.data$Team!="Totals")|> |
| 72 | +dplyr::mutate( |
| 73 | +dplyr::across( |
| 74 | +dplyr::where(is.character)&!dplyr::any_of(c("Team","type")), |
| 75 | +readr::parse_number |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | +team_stats_wide<-team_stats|> |
| 80 | +tidyr::pivot_wider( |
| 81 | +names_from=.data$type, |
| 82 | +values_from=-c(.data$Team,.data$type), |
| 83 | +names_sep="_" |
| 84 | + ) |
| 85 | + |
| 86 | +for_cols<- grep("_for$", names(team_stats_wide),value=TRUE) |
| 87 | +against_cols<- gsub("_for$","_against",for_cols) |
| 88 | +diff_cols<- gsub("_for$","_diff",for_cols) |
| 89 | + |
| 90 | +diff_list<-rlang::set_names( |
| 91 | +purrr::map2(for_cols,against_cols,~team_stats_wide[[.x]]-team_stats_wide[[.y]]), |
| 92 | +diff_cols |
| 93 | + ) |
| 94 | + |
| 95 | +team_stats_final<-dplyr::bind_cols(team_stats_wide,tibble::as_tibble(diff_list))|> |
| 96 | +dplyr::mutate(season=season)|> |
| 97 | +dplyr::relocate(.data$season,.before=.data$Team) |
| 98 | + |
| 99 | +if (summary_type=="averages") { |
| 100 | +results<-fitzRoy::fetch_results_afltables(season) |
| 101 | + |
| 102 | +game_counts<-results|> |
| 103 | +dplyr::filter(!is.na(.data$Home.Team),!is.na(.data$Away.Team))|> |
| 104 | +tidyr::pivot_longer(cols= c(.data$Home.Team,.data$Away.Team), |
| 105 | +names_to="HomeAway",values_to="Team")|> |
| 106 | +dplyr::count(.data$Team,name="Games") |
| 107 | + |
| 108 | +# fetch_results_afltables has these two teams named differently |
| 109 | +name_map<- c( |
| 110 | +"GWS"="Greater Western Sydney", |
| 111 | +"Footscray"="Western Bulldogs" |
| 112 | + ) |
| 113 | +game_counts<-game_counts|> |
| 114 | +dplyr::mutate( |
| 115 | +Team=dplyr::recode(.data$Team,!!!name_map) |
| 116 | + ) |
| 117 | + |
| 118 | +team_stats_final<-dplyr::left_join(team_stats_final,game_counts,by="Team") |
| 119 | + |
| 120 | +numeric_cols<-team_stats_final|> |
| 121 | +dplyr::select(-.data$season,-.data$Team,-.data$Games)|> |
| 122 | +dplyr::select(where(is.numeric))|> |
| 123 | + names() |
| 124 | + |
| 125 | +team_stats_final<-team_stats_final|> |
| 126 | +dplyr::mutate(dplyr::across(dplyr::all_of(numeric_cols),~./.data$Games)) |
| 127 | + } |
| 128 | + |
| 129 | +return(team_stats_final) |
| 130 | +} |