Low Level NHL API

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Low Level NHL API

If you would like to interact with the API directly without any helpers, the nhl_get_data() function is made for this purpose. It takes as input one or more urls (a character vector) and returns a list of the same length, each element containing the raw results returned from the API for the corresponding URL.

For example:

library(nhlapi)
urls <- c(
  "https://statsapi.web.nhl.com/api/v1/teams/1",
  "https://statsapi.web.nhl.com/api/v1/people/8477474"
)

nhl_get_data(urls)

Since it is helpful for work with R to get data in a flat data frame format, the function will by default attempt to flatten nested data frame columns and return flat data frames where possible. If you really want raw data returned, you can set the flatten argument to FALSE:

nhl_get_data(urls, flatten = FALSE)

In case there are errors encountered when retrieving the data, the process does not fail, but returns a list with class nhl_get_data_error:

nhl_get_data(c("https://statsapi.web.nhl.com/api/v1/wrongurl", urls))

The URLs themselves are preserved via the url attribute:

lapply(nhl_get_data(urls), attr, which = "url")


Try the nhlapi package in your browser

Any scripts or data that you put into this service are public.

nhlapi documentation built on Feb. 20, 2021, 9:06 a.m.