fastRhockey
fastRhockey copied to clipboard
expanded shift data
The function nhl_game_shifts
aggregates the data from https://api.nhle.com/stats/rest/en/shiftcharts?cayenneExp=gameId=
via a group_by function. This makes it harder to determine who is on the ice during any given event, in the play by play data as the player_id column is not maintained.
I suggest making a new standalone function that returns the detailed game shift data nhl_game_shifts_detailed
, this would make it easier to determine which players are on the ice for a given event.
something like this might fit with your code, and allow a users to find the player on the ice during any event.
nhl_game_shifts_by_player <- function (game_id)
{
base_url <- "https://api.nhle.com/stats/rest/en/shiftcharts?cayenneExp=gameId="
full_url <- paste0(base_url, game_id)
res <- httr::RETRY("GET", full_url)
fastRhockey:::check_status(res)
tryCatch(expr = {
res %>%
httr::content(as = "text", encoding = "UTF-8") |>
jsonlite::fromJSON() |>
magrittr::extract2('data') |>
dplyr::tibble() |> janitor::clean_names() |>
tidyr::unite("player_name", c(.data$first_name,
.data$last_name), sep = " ") |>
dplyr::select(.data$game_id,
.data$player_id, .data$player_name, .data$team_abbrev,
.data$team_id, .data$team_name, .data$period, .data$start_time,
.data$end_time, .data$duration) %>% dplyr::filter(!is.na(.data$duration)) |>
dplyr::mutate(start_time_ms = lubridate::ms(.data$start_time),
start_seconds = lubridate::period_to_seconds(.data$start_time_ms),
start_game_seconds = .data$start_seconds + (1200 * (.data$period - 1)), end_time_ms = lubridate::ms(.data$end_time),
end_seconds = lubridate::period_to_seconds(.data$end_time_ms),
end_game_seconds = .data$end_seconds + (1200 * (.data$period - 1)),
duration = lubridate::ms(.data$duration),
duration_seconds = lubridate::period_to_seconds(.data$duration)) |>
fastRhockey:::make_fastRhockey_data("detailed NHL Game Shifts Information from NHL.com", Sys.time())
}, error = function(e) {
message(glue::glue("{Sys.time()}: shift by player data for {game_id} generated an error, {e}."))
}, warning = function(w) {
}, finally = {
})
}