The goal of tibblify is to provide an easy way to convert a nested list into a tibble.
Install the released version of tibblify from CRAN:
install.packages("tibblify")Install the development version of tibblify from GitHub:
# install.packages("pak")
pak::pak("wranglezone/tibblify")To illustrate how tibblify() works, we’ll start with a list containing
information about four GitHub users.
library(tibblify)
gh_users_small <- purrr::map(gh_users, ~ .x[c("followers", "login", "url", "name", "location", "email", "public_gists")])
names(gh_users_small[[1]])
#> [1] "followers" "login" "url" "name" "location"
#> [6] "email" "public_gists"We can rectangle gh_users_small automatically with tibblify():
tibblify(gh_users_small)
#> The spec contains 1 unspecified field:
#> • email
#> # A tibble: 4 × 7
#> followers login url name location email public_gists
#> <int> <chr> <chr> <chr> <chr> <list> <int>
#> 1 780 jennybc https://api.github.co… Jenn… Vancouv… <NULL> 54
#> 2 3958 jtleek https://api.github.co… Jeff… Baltimo… <NULL> 12
#> 3 115 juliasilge https://api.github.co… Juli… Salt La… <NULL> 4
#> 4 213 leeper https://api.github.co… Thom… London,… <NULL> 46We can avoid the note about the unspecified field by formally providing
a spec starting with guess_tspec():
spec <- guess_tspec(gh_users_small, inform_unspecified = FALSE)
# Drop the unused email specification.
spec$fields$email <- NULL
tibblify(gh_users_small, spec = spec)
#> # A tibble: 4 × 6
#> followers login url name location public_gists
#> <int> <chr> <chr> <chr> <chr> <int>
#> 1 780 jennybc https://api.github.com/users… Jenn… Vancouv… 54
#> 2 3958 jtleek https://api.github.com/users… Jeff… Baltimo… 12
#> 3 115 juliasilge https://api.github.com/users… Juli… Salt La… 4
#> 4 213 leeper https://api.github.com/users… Thom… London,… 46Learn more in vignette("tibblify").
- jsonlite:
jsonlite::fromJSON()is excellent for parsing JSON, buttibblifyallows for strict specifications and can be faster for complex nested lists. - tidyr:
tidyr::hoist()andtidyr::unnest_longer()allow for step-by-step rectangling.tibblifyaims to rectangle the data in a single step based on a schema. - rrapply: Provides extended functionality for applying functions to nested lists, and can also be used to melt or prune lists.
- tidyjson: Offers a grammar for manipulating JSON data.
Please note that the tibblify project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.