NBAr - scrape NBA data from NBA.com API

May 5, 2017

NBAr

There was never a better time to be NBA, R and data analysis fan. Might be not the most useful tool nor the most exciting, but NBAr contains set of wrapper functions for downloading and simple processing of data from http://stats.nba.com API.

Over the last year functions contained in NBAr package helped my write my master thesis and also couple of blog posts on this site. Before I finally start writing more frequently I decided to finish the package and upload it on github so everyone can see it, use it, give feedback or contribute. Over the last year I have been slooowly working on functions contained in NBAr package

To install

if (!require("devtools"))
  install.packages("devtools")
devtools::install_github("PatrickChodowski/NBAr")

You can also check out my depository here

Example script

Script below enables you to get almost every table from Stats.Nba.com. I have been using it for a long time to create my own local data directories:

library(NBAr)
library(tidyverse)
#devtools::install_github("PatrickChodowski/NBAr")

season <- "2017"
gamelist<- paste("00",as.character(c(21700001:21701230)),sep="")

#########################
### Boxscores:
#########################

 traditional <- map(gamelist, ~getBoxscore(.,Type = 'traditional')) %>%
  compact() %>%  bind_rows()
 advanced <- map(gamelist, ~getBoxscore(.,Type = 'advanced')) %>%
   compact() %>%  bind_rows()
 playertrack <- map(gamelist, ~getBoxscore(.,Type = 'playertrack')) %>%
   compact() %>%  bind_rows()

#########################
### Play by play:
#########################

 playbyplay <- map(gamelist, ~getPlaybyplay(.)) %>% compact() %>%  bind_rows()

#########################
### Lineups:
#########################

 lineups <- getLineups(2017,5,'Base')
 bulls_lineups <- getLineups(2017,5,'Base', TeamID = 1610612741)

#########################
### On/Off court:
#########################

 onoff_bulls <- getOnOff(2017,1610612741)

#########################
### Players list
#########################

 players <- getPlayerlist(season)
 playerBio <- getPlayerBio(season)

 playerslist <- unique(players$PLAYER_ID)

#########################
### Shotcharts
#########################

 shots <- map(playerslist, ~getShotchart(.,Season = season)) %>%
   compact() %>%  bind_rows()

#########################
### Schedule
#########################

 schedule <- getSchedule(season)


#########################
### Tracking data
#########################

 MeasureType <- c("Drives","Defense","CatchShoot","Passing",
                  "Possessions","PullUpShot","Rebounding","Efficiency",
                  "SpeedDistance","ElbowTouch","PostTouch","PaintTouch")
 tracking <- map(MeasureType, ~getTracking(.,Season = season, Type = 'Team')) %>%
   compact() %>%  bind_cols()


#########################
### Playtype data
#########################

  Playtype <- c("Postup","Transition","Isolation","PRBallHandler",
                "PRRollman","Spotup","Handoff","Cut","OffScreen","OffRebound","Misc")
  plays <- map(Playtype, ~getPlaytype(.,Season = season, Type = 'Team')) %>%
    compact() %>%  bind_cols()


#########################
### Defense data
#########################

  DefenseCategory <- c("Overall","3+Pointers","2+Pointers",
                       "Less+Than+6Ft","Less+Than+10Ft","Greater+Than+15Ft")
  defense <- map(DefenseCategory, ~getDefense(.,Season = season, Type = 'Team')) %>%
    compact() %>%  bind_cols()

#########################
### General data
#########################

  MeasureType <- c("Base","Advanced","Misc","Scoring",
                   "Usage","Opponent","Defense", "Four+Factors")
  general <- map(MeasureType, ~getGeneral(.,Season = season, Type = 'Team')) %>%
    compact() %>%
    bind_cols()


#########################
### Hustle data
#########################

  hustle <- getHustle(season, Type = Type)

#########################
### Shooting data
#########################

  shooting <- getShooting(season, Type, "By+Zone", "Base")