knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
This is a package to transform large, multi-nested lists into a more user-friendly format (i.e. a tibble
) in R
. The initial focus is on making processing of return values from jsonlite::fromJSON()
queries more seamless, but ideally this package should be useful for deeply-nested lists from an array of sources.
Key features:
roomba()
searches deeply-nested list for names specified in cols
(a character vector) and returns a tibble
with the associated column titles. Nothing further about nesting hierarchy or depth need be specified.
Handles empty values gracefully by substituting NULL
values with NA
or user-specified value in default
, or truncates lists appropriately.
If you're only interested in sniffing out and replacing all NULL
s, turn to the replace_null()
function.
Option to keep
any
or all
data from the columns supplied
You can install the development version from GitHub with:
# install.packages("devtools") devtools::install_github("cstawitz/roomba")
Say we have some JSON from a pesky API.
library(roomba) json <- ' { "stuff": { "buried": { "deep": [ { "location": "here", "name": "Laura DeCicco", "super_power": "fixing merge conflicts", "other_secret_power": [] }, { "location": "here", "name": "Amanda Dobbyn", "super_power": "flight", "more_nested_stuff": 4 } ], "alsodeep": 2342423234, "stilldeep": { "even_deeper": [ { "location": "not here", "name": "Jim Hester", "super_power": [] }, { "location": "here", "name": "Christine Stawitz", "super_power": "invisibility", "more_nested_stuff": 5 }, { "location": "here", "name": "Isabella Velasquez", "super_power": "teleportation" } ] } } } }'
The JSON becomes a nested R list,
super_data <- json %>% jsonlite::fromJSON(simplifyVector = FALSE)
which we can pull data into the columns we want with roomba
.
super_data %>% roomba(cols = c("name", "super_power", "more_nested_stuff"), keep = any)
Let's try with a real-world Twitter example (see package data to use this data).
roomba(twitter_data, c("created_at", "name"))
Run the app like this:
shiny_roomba()
Feast your eyes on the original super_data
list!
super_data
And just the first element of the twitter
dataset r emo::ji("scream")
twitter_data[[1]]
Happy cleaning!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.