Convert a data.frame to a 'd3.js' Hierarchy

d3_nest(data = NULL, value_cols = character(), root = "root", json = TRUE)

Arguments

data

data.frame or data.frame derivative, such as tibble

value_cols

character vector with the names of the columns to use as data

root

character name of the root level of the hierarchy

json

logical to return as JSON

Value

nested data.frame

Examples

# convert Titanic to a nested d3 hierarchy

# devtools::install_github("timelyportfolio/d3r")
library(d3r)
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.1.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

titanic_df <- data.frame(Titanic)
tit_tb <- titanic_df %>%
  select(Class,Age,Survived,Sex,Freq) %>%
  d3_nest(value_cols="Freq", root="titanic")

tit_tb
#> {"children":[{"name":"1st","children":[{"name":"Child","children":[{"name":"No","children":[{"name":"Male","Freq":0,"colname":"Sex"},{"name":"Female","Freq":0,"colname":"Sex"}],"colname":"Survived"},{"name":"Yes","children":[{"name":"Male","Freq":5,"colname":"Sex"},{"name":"Female","Freq":1,"colname":"Sex"}],"colname":"Survived"}],"colname":"Age"},{"name":"Adult","children":[{"name":"No","children":[{"name":"Male","Freq":118,"colname":"Sex"},{"name":"Female","Freq":4,"colname":"Sex"}],"colname":"Survived"},{"name":"Yes","children":[{"name":"Male","Freq":57,"colname":"Sex"},{"name":"Female","Freq":140,"colname":"Sex"}],"colname":"Survived"}],"colname":"Age"}],"colname":"Class"},{"name":"2nd","children":[{"name":"Child","children":[{"name":"No","children":[{"name":"Male","Freq":0,"colname":"Sex"},{"name":"Female","Freq":0,"colname":"Sex"}],"colname":"Survived"},{"name":"Yes","children":[{"name":"Male","Freq":11,"colname":"Sex"},{"name":"Female","Freq":13,"colname":"Sex"}],"colname":"Survived"}],"colname":"Age"},{"name":"Adult","children":[{"name":"No","children":[{"name":"Male","Freq":154,"colname":"Sex"},{"name":"Female","Freq":13,"colname":"Sex"}],"colname":"Survived"},{"name":"Yes","children":[{"name":"Male","Freq":14,"colname":"Sex"},{"name":"Female","Freq":80,"colname":"Sex"}],"colname":"Survived"}],"colname":"Age"}],"colname":"Class"},{"name":"3rd","children":[{"name":"Child","children":[{"name":"No","children":[{"name":"Male","Freq":35,"colname":"Sex"},{"name":"Female","Freq":17,"colname":"Sex"}],"colname":"Survived"},{"name":"Yes","children":[{"name":"Male","Freq":13,"colname":"Sex"},{"name":"Female","Freq":14,"colname":"Sex"}],"colname":"Survived"}],"colname":"Age"},{"name":"Adult","children":[{"name":"No","children":[{"name":"Male","Freq":387,"colname":"Sex"},{"name":"Female","Freq":89,"colname":"Sex"}],"colname":"Survived"},{"name":"Yes","children":[{"name":"Male","Freq":75,"colname":"Sex"},{"name":"Female","Freq":76,"colname":"Sex"}],"colname":"Survived"}],"colname":"Age"}],"colname":"Class"},{"name":"Crew","children":[{"name":"Child","children":[{"name":"No","children":[{"name":"Male","Freq":0,"colname":"Sex"},{"name":"Female","Freq":0,"colname":"Sex"}],"colname":"Survived"},{"name":"Yes","children":[{"name":"Male","Freq":0,"colname":"Sex"},{"name":"Female","Freq":0,"colname":"Sex"}],"colname":"Survived"}],"colname":"Age"},{"name":"Adult","children":[{"name":"No","children":[{"name":"Male","Freq":670,"colname":"Sex"},{"name":"Female","Freq":3,"colname":"Sex"}],"colname":"Survived"},{"name":"Yes","children":[{"name":"Male","Freq":192,"colname":"Sex"},{"name":"Female","Freq":20,"colname":"Sex"}],"colname":"Survived"}],"colname":"Age"}],"colname":"Class"}],"name":"titanic"} 

# see as tibble
titanic_df %>%
  select(Class,Age,Survived,Sex,Freq) %>%
  d3_nest(value_cols="Freq", root="titanic", json=FALSE)
#> # A tibble: 1 x 2
#>   children         name   
#>   <list>           <chr>  
#> 1 <tibble [4 x 3]> titanic

# see the structure with listviewer
tit_tb %>%
  listviewer::jsonedit()
if (FALSE) { library(treemap) library(d3r) library(dplyr) library(tidyr) treemap::random.hierarchical.data() %>% d3_nest(value_cols = "x") # use example from ?treemap data(GNI2014) treemap( GNI2014, index=c("continent", "iso3"), vSize="population", vColor="GNI", type="value", draw=FALSE ) %>% {.$tm} %>% select(continent,iso3,color,vSize) %>% d3_nest(value_cols = c("color", "vSize")) }