Using the NHL API

· Read in about 4 min · (744 words) ·

NHL Standings

The past few weeks I have found myself exploring the NHL API. Learning how to use their API and piece together to url’s in order to return the information I want has been challenging and so rewarding when it works. I have been using R and the ‘jsonlite’ package to make these requests. Below I want to document, some of the requests I have made along with cleaning up some of the data. The main purpose of this post and many of my future posts will be to document how I did something so that I have a place to go back and reference it. I have found that the more and more coding resources I find, the more clutered my files get. Learning how to properly organize these files is becoming increasingly more important as I look to reference them in the future.

In addition to writing about the NHL API, I will be using Rmarkdown to write these reports. My hope is that by doing so, overtime these resources will be come better and better.

The frist thing I always do is load the packages I use. As I write more and more code below, I will add additional packages to this code chunk.

Below is the code I used to pull the national hockey league standings using the nhl api. These are the standings as of 2019-03-03.

url <- "https://statsapi.web.nhl.com/api/v1/standings"
standings <- fromJSON(url, flatten = TRUE)

divisions <- 1:4
standings_df <- data.frame()
for (i in 1:length(divisions)) {
  all_standings <- standings$records$teamRecords[[i]]
  standings_df <- rbind(standings_df, all_standings)
}
  
#View(standings_df)

standings_table <- standings_df %>%
  arrange(desc(points)) %>% 
  mutate(GD = goalsScored - goalsAgainst) %>% 
  select(Team = team.name, 
         GP = gamesPlayed,
         W = leagueRecord.wins, 
         L = leagueRecord.losses, 
         OTL = leagueRecord.ot, 
         PTS = points, 
         ROW = row,
         GF = goalsScored,
         GA = goalsAgainst,
         GD,
         STREAK = streak.streakCode)

standings_table %>% 
  kable(align = c("l", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c")) %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "bordered"), full_width = T, position = "center", font_size = 12) %>% 
  column_spec(c(1,6), bold = T) %>% 
  row_spec(0, bold = T, color = "white", background = "#213456")
Team GP W L OTL PTS ROW GF GA GD STREAK
Tampa Bay Lightning 66 50 12 4 104 44 258 174 84 W1
Calgary Flames 65 41 17 7 89 41 232 186 46 L1
Boston Bruins 65 39 17 9 87 37 194 161 33 W3
San Jose Sharks 66 39 19 8 86 39 238 206 32 W2
Toronto Maple Leafs 65 40 21 4 84 40 233 186 47 W1
Washington Capitals 66 38 21 7 83 34 226 208 18 W4
Winnipeg Jets 65 39 22 4 82 37 222 193 29 W2
New York Islanders 65 37 21 7 81 34 187 157 30 L2
Nashville Predators 68 38 25 5 81 35 202 180 22 W1
Carolina Hurricanes 65 36 23 6 78 35 196 176 20 W5
Pittsburgh Penguins 65 34 22 9 77 33 226 202 24 W1
Montréal Canadiens 66 35 24 7 77 33 201 193 8 L1
Vegas Golden Knights 67 36 26 5 77 33 201 187 14 W4
Columbus Blue Jackets 65 36 26 3 75 36 203 197 6 L2
St. Louis Blues 65 34 25 6 74 33 187 181 6 L2
Philadelphia Flyers 66 32 26 8 72 30 201 219 -18 W2
Dallas Stars 65 33 27 5 71 33 166 170 -4 W2
Minnesota Wild 66 32 27 7 71 31 182 191 -9 OT1
Arizona Coyotes 65 32 28 5 69 28 176 183 -7 W6
Buffalo Sabres 66 30 28 8 68 26 190 211 -21 L2
Colorado Avalanche 66 28 26 12 68 27 216 208 8 L2
Florida Panthers 65 28 26 11 67 25 207 224 -17 L1
New York Rangers 65 27 27 11 65 21 190 218 -28 OT1
Edmonton Oilers 66 29 30 7 65 26 187 216 -29 W3
Chicago Blackhawks 66 27 30 9 63 26 220 250 -30 L2
Vancouver Canucks 66 27 30 9 63 24 180 206 -26 L2
Anaheim Ducks 66 25 32 9 59 22 144 203 -59 W1
New Jersey Devils 66 25 33 8 58 24 188 223 -35 L3
Los Angeles Kings 65 24 33 8 56 22 158 207 -49 W1
Detroit Red Wings 65 23 33 9 55 21 179 223 -44 L4
Ottawa Senators 66 23 38 5 51 23 195 246 -51 W1