-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathjson-paging.Rmd.orig
92 lines (69 loc) · 3.67 KB
/
json-paging.Rmd.orig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "Combining pages of JSON data with jsonlite"
date: "`r Sys.Date()`"
output:
html_document
vignette: >
%\VignetteIndexEntry{Combining pages of JSON data with jsonlite}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
```{r echo=FALSE}
library(knitr)
opts_chunk$set(comment="")
#this replaces tabs by spaces because latex-verbatim doesn't like tabs
toJSON <- function(...){
gsub("\t", " ", jsonlite::toJSON(...), fixed=TRUE);
}
```
```{r echo=FALSE, message=FALSE}
library(jsonlite)
```
The [jsonlite](https://cran.r-project.org/package=jsonlite) package is a `JSON` parser/generator for R which is optimized for pipelines and web APIs. It is used by the OpenCPU system and many other packages to get data in and out of R using the `JSON` format.
## A bidirectional mapping
One of the main strengths of `jsonlite` is that it implements a bidirectional [mapping](http://arxiv.org/abs/1403.2805) between JSON and data frames. Thereby it can convert nested collections of JSON records, as they often appear on the web, immediately into the appropriate R structure. For example to grab some data from ProPublica we can simply use:
```{r eval=FALSE}
library(jsonlite)
mydata <- fromJSON("https://projects.propublica.org/forensics/geos.json", flatten = TRUE)
View(mydata)
```
The `mydata` object is a data frame which can be used directly for modeling or visualization, without the need for any further complicated data manipulation.
## Paging with jsonlite
A question that comes up frequently is how to combine pages of data. Most web APIs limit the amount of data that can be retrieved per request. If the client needs more data than what can fits in a single request, it needs to break down the data into multiple requests that each retrieve a fragment (page) of data, not unlike pages in a book. In practice this is often implemented using a `page` parameter in the API. Below an example from the [ProPublica Nonprofit Explorer API](https://projects.propublica.org/nonprofits/api) where we retrieve the first 3 pages of tax-exempt organizations in the USA, ordered by revenue:
```{r}
baseurl <- "https://projects.propublica.org/nonprofits/api/v2/search.json?order=revenue&sort_order=desc"
mydata0 <- fromJSON(paste0(baseurl, "&page=0"), flatten = TRUE)
mydata1 <- fromJSON(paste0(baseurl, "&page=1"), flatten = TRUE)
mydata2 <- fromJSON(paste0(baseurl, "&page=2"), flatten = TRUE)
#The actual data is in the organizations element
mydata0$organizations[1:10, c("name", "city", "strein")]
```
To analyze or visualize these data, we need to combine the pages into a single dataset. We can do this with the `rbind_pages` function. Note that in this example, the actual data is contained by the `organizations` field:
```{r}
#Rows per data frame
nrow(mydata0$organizations)
#Combine data frames
organizations <- rbind_pages(
list(mydata0$organizations, mydata1$organizations, mydata2$organizations)
)
#Total number of rows
nrow(organizations)
```
## Automatically combining many pages
We can write a simple loop that automatically downloads and combines many pages. For example to retrieve the first 20 pages with non-profits from the example above:
```{r, message=FALSE}
#store all pages in a list first
baseurl <- "https://projects.propublica.org/nonprofits/api/v2/search.json?order=revenue&sort_order=desc"
pages <- list()
for(i in 0:20){
mydata <- fromJSON(paste0(baseurl, "&page=", i))
message("Retrieving page ", i)
pages[[i+1]] <- mydata$organizations
}
#combine all into one
organizations <- rbind_pages(pages)
#check output
nrow(organizations)
colnames(organizations)
```
From here, we can go straight to analyzing the organizations data without any further tedious data manipulation.