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)
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:13 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:14 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:15 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:15 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:16 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:17 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> 05:28:17 | E | The following 2 of 2 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/teams/1$ https://statsapi.web.nhl.com/api/v1/people/8477474
#> [[1]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'>
#>
#> attr(,"class")
#> [1] "list" "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#>
#> [[2]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'>
#>
#> attr(,"class")
#> [1] "list" "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"
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)
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:17 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:18 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:19 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:20 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:21 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:22 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> 05:28:22 | E | The following 2 of 2 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/teams/1$ https://statsapi.web.nhl.com/api/v1/people/8477474
#> [[1]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'>
#>
#> attr(,"class")
#> [1] "list" "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#>
#> [[2]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'>
#>
#> attr(,"class")
#> [1] "list" "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"
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))
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/wrongurl': status was 'Couldn't resolve
#> host name'
#> 05:28:22 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/wrongurl error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/wrongurl': status was 'Couldn't resolve
#> host name'
#> 05:28:23 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/wrongurl error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/wrongurl': status was 'Couldn't resolve
#> host name'
#> 05:28:24 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/wrongurl error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:24 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:25 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:26 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:27 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:28 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:29 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> 05:28:29 | E | The following 3 of 3 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/wrongurl$ https://statsapi.web.nhl.com/api/v1/teams/1$ https://statsapi.web.nhl.com/api/v1/people/8477474
#> [[1]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'>
#>
#> attr(,"class")
#> [1] "list" "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/wrongurl"
#>
#> [[2]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'>
#>
#> attr(,"class")
#> [1] "list" "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#>
#> [[3]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'>
#>
#> attr(,"class")
#> [1] "list" "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"
The URLs themselves are preserved via the url
attribute:
lapply(nhl_get_data(urls), attr, which = "url")
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:29 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:30 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 05:28:31 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:31 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:32 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 05:28:33 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> 05:28:33 | E | The following 2 of 2 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/teams/1$ https://statsapi.web.nhl.com/api/v1/people/8477474
#> [[1]]
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#>
#> [[2]]
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"