Example of the stattle function to auto-page the results and parse.
## simple setup
options(stringsAsFactors = F)
knitr::opts_chunk$set(comment = NA)
## packages
library(httr)
library(dplyr)
library(jsonlite)
## token
TOKEN = "YOURTOKENHERE"
## source the function
# source("testing-walk-api.R")
## source the functions
u = "https://gist.githubusercontent.com/Btibert3/8f3671af30c8d1afc555/raw/4d620b99cd6c8df3aa17a6c84ac07056a5d479f3/queryAPI.r"
devtools::source_url(u)
u = "https://gist.githubusercontent.com/Btibert3/8f3671af30c8d1afc555/raw/4d620b99cd6c8df3aa17a6c84ac07056a5d479f3/stattle.r"
devtools::source_url(u)
Get the teams
teams = queryAPI(TOKEN, ep="teams", verbose=T)
what came back
names(teams)
[1] "response" "api_json"
names(teams$api_json)
[1] "divisions" "conferences" "leagues" "teams"
pull something back. Let's just do the conferences.
teams$api_json$conferences
id created_at
1 b6a5910b-bd9b-4fb3-b7e5-654c2ae17fa3 2015-11-29T03:33:56Z
2 95e3218b-fa5e-4bb8-a6ff-acb61da1876c 2015-11-29T03:33:56Z
updated_at name
1 2015-11-29T03:33:56Z Eastern Conference
2 2015-11-29T03:33:56Z Western Conference
league_id
1 48803e9e-c4ec-414a-a85d-9abfa6e9cfd9
2 48803e9e-c4ec-414a-a85d-9abfa6e9cfd9
how many responses came back?
teams$response$headers$total
[1] "30"
Use the stattle wrapper function to walk the data for the end user, but calling queryAPI internally.
nhl_teams = stattle(TOKEN, ep='teams', walk=T)
length(nhl_teams)
[1] 2
names(nhl_teams[[1]])
[1] "divisions" "conferences" "leagues" "teams"
This is what was expected. Collapse the data into one dataframe
dat = data.frame()
for (i in 1:length(nhl_teams)) {
tmp_dat = nhl_teams[[i]]$teams
dat = bind_rows(dat, tmp_dat)
rm(tmp_dat)
}
print the first few rows
dat = as.data.frame(dat)
head(dat)
id created_at
1 54410a6b-c9e0-443e-a4cf-b3e22f2cbb75 2015-11-29T03:33:56Z
2 26390f10-d08d-4a78-acf6-ac0a5d31a78c 2015-11-29T03:33:56Z
3 b0c4bd8e-fba7-4374-87d2-68bd4ab20875 2015-11-29T03:33:56Z
4 c94be617-bf0c-4516-82b8-782d7aa783ae 2015-11-29T03:33:56Z
5 1f2dbc61-cced-4d84-9ee9-81bae4c9c70f 2015-11-29T03:33:56Z
6 d32b9696-2d78-40a0-bf6d-3cd16dc48c96 2015-11-29T03:33:56Z
updated_at division_label league_abbreviation
1 2015-11-29T03:33:56Z Eastern Conference Metropolitan NHL
2 2015-11-29T03:33:56Z Eastern Conference Metropolitan NHL
3 2015-11-29T03:33:56Z Eastern Conference Metropolitan NHL
4 2015-11-29T03:33:56Z Eastern Conference Metropolitan NHL
5 2015-11-30T18:33:47Z Eastern Conference Metropolitan NHL
6 2015-11-29T03:33:56Z Eastern Conference Metropolitan NHL
league_name location name nickname
1 National Hockey League Columbus Columbus Blue Jackets
2 National Hockey League Pittsburgh Pittsburgh Penguins
3 National Hockey League Carolina Carolina Hurricanes
4 National Hockey League Philadelphia Philadelphia Flyers
5 National Hockey League New York NY Islanders Islanders
6 National Hockey League New York NY Rangers Rangers
slug
1 clb
2 pit-e1b87e73-3b07-43c9-8bd4-30c02b7bf4e1
3 car-4c5f0626-f9b5-447d-a72f-128b78d92c21
4 phi-97f229a7-f339-4afc-a33a-e6d5b55b507c
5 nyi
6 nyr
division_id
1 2a45d995-a56b-43c8-838d-700e5cf1f922
2 2a45d995-a56b-43c8-838d-700e5cf1f922
3 2a45d995-a56b-43c8-838d-700e5cf1f922
4 2a45d995-a56b-43c8-838d-700e5cf1f922
5 2a45d995-a56b-43c8-838d-700e5cf1f922
6 2a45d995-a56b-43c8-838d-700e5cf1f922
league_id
1 48803e9e-c4ec-414a-a85d-9abfa6e9cfd9
2 48803e9e-c4ec-414a-a85d-9abfa6e9cfd9
3 48803e9e-c4ec-414a-a85d-9abfa6e9cfd9
4 48803e9e-c4ec-414a-a85d-9abfa6e9cfd9
5 48803e9e-c4ec-414a-a85d-9abfa6e9cfd9
6 48803e9e-c4ec-414a-a85d-9abfa6e9cfd9
The code above used a wrapper function to lower the threshold to walk the api and parse, after the walk.
Can be refactored further, but hopefully this is moving in the right direction.
ISSUE
The two files sourced above work together, but I ran into some wierd combination of the files where I was getting an error on bind_rows. The error was not compatiable with STRSXP, which is bizarre. Right now, I think the two files work together, but I will need to investigate further as we go deeper.
sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] jsonlite_0.9.19 dplyr_0.4.3 httr_1.0.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.2 digest_0.6.8 assertthat_0.1 R6_2.1.1
[5] DBI_0.3.1.9008 git2r_0.10.1 formatR_1.2 magrittr_1.5
[9] evaluate_0.7 stringi_1.0-1 curl_0.9.3 xml2_0.1.2
[13] rmarkdown_0.7 devtools_1.8.0 tools_3.2.0 stringr_1.0.0
[17] yaml_2.1.13 parallel_3.2.0 rversions_1.0.2 memoise_0.2.1
[21] htmltools_0.2.6 knitr_1.10.5