spread_all | R Documentation |
Like the spread
function in tidyr
but for JSON,
this function spreads out any JSON objects that are scalars into new columns.
If objects are nested, then the recursive flag will expand scalar values of
nested objects out with a compound column name based on the sequences of
nested object names concatenated with the sep
character.
spread_all(.x, recursive = TRUE, sep = ".")
.x |
a json string or |
recursive |
whether or not to recursively spread nested objects |
sep |
character used to separate nested object names when resursive
is |
Note that arrays are ignored by this function, use gather_array
to gather the array first, and then use spread_all
if the array
contains objects or use one of the append_values
functions to
capture the array values if they are scalars.
Note that scalar JSON values (e.g., a JSON string like '1') are also ignored, as they have no names to create column names with.
The order of columns is determined by the order they are encountered in the JSON document, with nested objects placed at the end.
If an objects have name-value pairs with names that are duplicates, then
".n"
is appended for n incrementing from 2 on to ensure that columns
are unique. This also happens if .x
already has a column with the
name of a name-value pair.
This function does not change the value of the JSON attribute of the
tbl_json
object in any way.
a tbl_json
object
spread_values
to specific which specific values
to spread along with their types,
spread
for spreading data frames
# A simple example json <- c('{"a": "x", "b": 1, "c": true}', '{"a": "y", "c": false}', '{"a": null, "d": "z"}') json %>% spread_all # A more complex example worldbank %>% spread_all ## Not run: # Resolving duplicate column names json <- '{"a": "x", "a": "y"}' json %>% spread_all ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.