# NOT RUN {
# Retrieve some game feed data
gameFeeds <- lapply(
2019010001:2019010010,
nhlapi::nhl_games_feed
)
# Create a data.frame with plays
getPlaysDf <- function(gm) {
playsRes <- try(gm[[1L]][["liveData"]][["plays"]][["allPlays"]])
if (inherits(playsRes, "try-error")) data.frame() else playsRes
}
plays <- lapply(gameFeeds, getPlaysDf)
plays <- nhlapi:::util_rbindlist(plays)
plays <- plays[!is.na(plays$coordinates.x), ]
# Move the coordinates to non-negative values before plotting
plays$coordx <- plays$coordinates.x + abs(min(plays$coordinates.x))
plays$coordy <- plays$coordinates.y + abs(min(plays$coordinates.y))
# Select goals only
goals <- plays[plays$result.event == "Goal", ]
# Create the plot and add goals
nhlapi::plot_rink()
points(goals$coordinates.x, goals$coordinates.y)
# }
Run the code above in your browser using DataLab