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

View source: R/hierarchy.R

d3_nestR Documentation

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

Description

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

Usage

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)

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

tit_tb

# see as tibble
titanic_df %>%
  select(Class,Age,Survived,Sex,Freq) %>%
  d3_nest(value_cols="Freq", root="titanic", json=FALSE)

# see the structure with listviewer
tit_tb %>%
  listviewer::jsonedit()
## Not run: 
  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"))

## End(Not run)

d3r documentation built on Oct. 2, 2023, 5:08 p.m.

Related to d3_nest in d3r...