R/nhl_get_game.R

Defines functions nhl_get_game

Documented in nhl_get_game

#' Function to return all raw data from a single NHL game.  plays is a somewhat tidied data.frame of play-by-play data; players is a list of the player metadata associated with each play.  raw is the full raw data returned from the nhl api.
#'
#'
#' @param game_id raw game_id; can be found with nhl_get_team_games().
#' @import jsonlite
#'
#' @examples
#' game <- nhl_get_game(game_id = 2018020385)

#' @export
nhl_get_game <- function(game_id) {

  raw <- fromJSON(
    paste0('https://statsapi.web.nhl.com/api/v1/game/', game_id,
           '/feed/live')
  )

  dfs <- list(
    raw$liveData$plays$allPlays$coordinates,
    raw$liveData$plays$allPlays$about,
    raw$liveData$plays$allPlays$result
  )

  plays <- do.call(cbind, dfs)

  out <- list(
    plays   = plays,
    players = raw$liveData$plays$allPlays$players,
    raw     = raw
  )
  return(out)
}
alexpavlakis/nhl documentation built on May 18, 2019, 2:35 p.m.