Updated the queryAPI
and created a new function, stattle
that wraps it and makes walking easier. jsonlite
is great, but I started to bump
into some issues with dplyr::bind_rows
. Right now these two functions play nice together, but needs to be further tested.
## source the functions
devtools::source_url('https://gist.githubusercontent.com/tcash21/44667f7b8578cc8e061e/raw/fdac84a0a445e448427cc3557e8c9393ce1fa470/queryAPI.r')
devtools::source_url('https://gist.githubusercontent.com/tcash21/44667f7b8578cc8e061e/raw/69503a2dcd69a5a2fc5021c25a7bbf86c1080aa9/stattle.r')
We will handle these better shortly, but the code requires:
dplyr
httr
jsonlite
league = "nba"
sport = "basketball"
q_body = list(player_id="nba-rajon-rondo")
ep = "game_logs"
rondo.logs <- stattle(TOKEN, sport=sport, league=league, ep=ep, query=q_body, version=1, walk=T,verbose=T)
The rondo.logs
object returns a list of lists. In the example above, there were two hits to the API, so length(rondo.logs)
is 2
. Nested below each of these parent lists are the data.
Collapse the data into a dataframe.
plus.minus <- unlist(lapply(rondo.logs, function(x) x$game_log$plus_minus))
turnovers <- unlist(lapply(rondo.logs, function(x) x$game_log$turnovers))
game.name <- unlist(lapply(rondo.logs, function(x) x$games$name))
d <- data.frame(plus_minus = plus.minus, turnovers=turnovers, game=game.name)
Get all NBA triple doubles this season
league = "nba"
sport = "basketball"
ep = "stats"
q_body=list(stat="triple_double")
tripdubs = stattle(TOKEN, sport=sport, league=league, ep=ep, query=q_body, version=1, walk=T)
- put this into a more formal package
- leverage way better version control
I had 2nd
devtools::source_url
throw an error on me for a bad file??? Wierd. This worked on my machine, so not sure if it is setting error or whatever random bug it could be.As I type that, I wonder if I had an old browser page open and a change was pushed. That could have been on my end ...