Skip to content

Commit

Permalink
Add bind_rows support
Browse files Browse the repository at this point in the history
Update multiple-apis.Rmd
  • Loading branch information
colearendt committed May 19, 2017
1 parent eae3660 commit f293b65
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
14 changes: 14 additions & 0 deletions R/tbl_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ slice_.tbl_json <- wrap_dplyr_verb(dplyr::slice_)
#' @method slice tbl_json
slice.tbl_json <- wrap_dplyr_verb(dplyr::slice)

#' @export
bind_rows <- function(...) {
r <- dplyr::bind_rows(...)

d <- list_or_dots(...)
if (all(unlist(lapply(d,is.tbl_json)))) {
j <- unlist(lapply(d, attr, 'JSON'), recursive=FALSE)
return(tbl_json(r,j))
} else {
warning('Some non-tbl_json objects. Reverting to dplyr::bind_rows')
return(r)
}
}

#' Convert the JSON in an tbl_json object back to a JSON string
#'
#' @param x a tbl_json object
Expand Down
44 changes: 44 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,47 @@ rbind_tbl_json <- function(x, y) {
)

}


#' Handles dots or a list
list_or_dots <- function (...)
{
dots <- list(...)
data_lists <- vapply(dots, is_data_list, logical(1))
dots[data_lists] <- lapply(dots[data_lists], list)
unlist(dots, recursive = FALSE)
}

#'
#' Checks whether a list is being provided
#'
is_data_list <- function (x)
{
if (is.data.frame(x) || is.null(x))
return(TRUE)
if (!is.list(x))
return(FALSE)
if (!is.null(names(x)) && length(x) == 0)
return(TRUE)
if (any(!has_names(x)))
return(FALSE)
is_1d <- vapply(x, is_1d, logical(1))
if (any(!is_1d))
return(FALSE)
n <- vapply(x, length, integer(1))
if (any(n != n[1]))
return(FALSE)
TRUE
}

#' Check for Names
has_names <- function (x)
{
nms <- names(x)
if (is.null(nms)) {
rep(FALSE, length(x))
}
else {
!is.na(nms) & nms != ""
}
}
6 changes: 6 additions & 0 deletions packrat/packrat.lock
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ Source: CRAN
Version: 1.0.0
Hash: 77da8f1df873a4b91e5c4a68fe2fb1b6

Package: pryr
Source: CRAN
Version: 0.1.2
Hash: 4189249ad9cfa35bb1f70ce398fce673
Requires: Rcpp, stringr

Package: purrr
Source: CRAN
Version: 0.2.2.2
Expand Down
11 changes: 7 additions & 4 deletions vignettes/multiple-apis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ library(tidyjson)
Tidyverse is used heavily for data cleansing. Let's explore some of the data through Github's APIs. We are going to grab the data directly and then explore the structure of the JSON with `json_schema`.

```{r, echo=TRUE}
dplyr_issues <- as.tbl_json('https://api.github.com/repos/tidyverse/dplyr/issues')
baseurl <- 'https://api.github.com/repos/tidyverse/dplyr/issues'
dplyr_issues <- as.tbl_json(baseurl)
dplyr_issues %>% json_schema %>% prettify
```

After exploring the structure of the data, we decide we want to look at a high level of what sort of issues we have.
After exploring the structure of the data, we decide we want to look at a high level of the isssues we have.

```{r, echo=TRUE}
Expand Down Expand Up @@ -77,9 +78,11 @@ highlevel %>% group_by(state) %>% summarize(nissues=n())
```

Let's aggregate a few more api calls. Documentation can be found at the [github API docs](https://developer.github.com/guides/traversing-with-pagination/).
Let's aggregate a few more api calls. Documentation can be found at the [github API docs](https://developer.github.com/guides/traversing-with-pagination/) and in particular [here](https://developer.github.com/v3/issues/#list-issues).

```{r, echo=TRUE}
manyissues <- lapply(c(1:7), function(x){as.tbl_json(paste0(baseurl,'?state=all&per_page=50&page=',x))})
## Collapse into one tbl_json
manyissues <- bind_rows(manyissues)
```

0 comments on commit f293b65

Please sign in to comment.