Return nested list with nested level and value
Asked Answered
E

2

8

I would like to visualize some deeply nested data using networkD3. I can't figure out how to get the data into the right format before sending to radialNetwork.

Here is some sample data:

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

where level indicates the level of the nest, and value is the name of the node. By using these two vectors, I need to get the data into the following format:

my_list <- list(
  name = "root",
  children = list(
    list(
      name = value[1], ## a
      children = list(list(
        name = value[2], ## b
        children = list(list(
          name = value[3], ## c
          children = list(
            list(name = value[4]), ## d
            list(name = value[5]) ## e
          )
        ),
        list(
          name = value[6], ## f
          children = list(
            list(name = value[7]), ## g
            list(name = value[8]) ## h
          )
        ))
      ))
    ),
    list(
      name = value[9], ## i
      children = list(list(
        name = value[10], ## j
        children = list(list(
          name = value[11] ## k
        ))
      ))
    )
  )
)

Here is the deparsed object:

> dput(my_list)
# structure(list(name = "root",
#                children = list(
#                  structure(list(
#                    name = "a",
#                    children = list(structure(
#                      list(name = "b",
#                           children = list(
#                             structure(list(
#                               name = "c", children = list(
#                                 structure(list(name = "d"), .Names = "name"),
#                                 structure(list(name = "e"), .Names = "name")
#                               )
#                             ), .Names = c("name",
#                                           "children")), structure(list(
#                                             name = "f", children = list(
#                                               structure(list(name = "g"), .Names = "name"),
#                                               structure(list(name = "h"), .Names = "name")
#                                             )
#                                           ), .Names = c("name",
#                                                         "children"))
#                           )), .Names = c("name", "children")
#                    ))
#                  ), .Names = c("name",
#                                "children")), structure(list(
#                                  name = "i", children = list(structure(
#                                    list(name = "j", children = list(structure(
#                                      list(name = "k"), .Names = "name"
#                                    ))), .Names = c("name",
#                                                    "children")
#                                  ))
#                                ), .Names = c("name", "children"))
#                )),
#           .Names = c("name",
#                      "children"))

Then I can pass it to the final plotting function:

library(networkD3)
radialNetwork(List = my_list)

The output will look similar to this:

enter image description here


Question: How can I create the nested list?

Note: As pointed out by @zx8754, there is already a solution in this SO post, but that requires data.frame as input. Due to the inconsistency in my level, I don't see a simple way to transform it into a data.frame.

Enate answered 15/12, 2016 at 5:54 Comment(3)
@zx8754 Added dput(my_list). In addition, the input data is not data.frame, and making it into data.frame is not easy IMO, because the levels are not consistent. That's why I tagged recursion and thinking it might be the direction. However, correct me if I am wrong.Enate
We need a recursive function that would take dataframe and split on minimum value, sorry no time to code at the moment. Something like: df1 <- data.frame(level, value, stringsAsFactors = FALSE); split(df1, cumsum(df1$level == 1)) then remove the min values, and split on next min value, etc.Bale
I was thinking about that too, but wasn't sure how to tag each children to the correct parents. In other words, how could we prevent tagging the 2nd level 2 value to the 1st parent.Enate
A
5

Using a data.table-style merge:

library(data.table)
dt = data.table(idx=1:length(value), level, parent=value)

dt = dt[dt[, .(i=idx, level=level-1, child=parent)], on=.(level, idx < i), mult='last']

dt[is.na(parent), parent:= 'root'][, c('idx','level'):= NULL]

> dt
#     parent child
#  1:   root     a
#  2:      a     b
#  3:      b     c
#  4:      c     d
#  5:      c     e
#  6:      b     f
#  7:      f     g
#  8:      f     h
#  9:   root     i
# 10:      i     j
# 11:      j     k

Now we can use the solution from the other post:

x = maketreelist(as.data.frame(dt))

> identical(x, my_list)
# [1] TRUE
Aegaeon answered 18/12, 2016 at 6:41 Comment(2)
This is awesome. Thanks! I am trying to understand the code, so a quick question for you: Is your second line something like a cross join and filtered by the last row of level?Enate
Np. Second line is a non-equi join filtered by last match. See channel9.msdn.com/Events/useR-international-R-User-conference/…Aegaeon
F
3

As a preface, your data is difficult to work with because critical information is encoded in the order of the values in level. I don't know how you get those values in that order, but consider that there may be a better way to structure that information in the first place, which would make the next task easier.

Here's a base-y way of converting your data into a data frame with 2 columns, parent and child, then passing that into data.tree functions that can easily convert to the JSON format you need... and then pass it on to radialNetwork...

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(data.tree)
library(networkD3)

parent_idx <- sapply(1:length(level), function(n) rev(which(level[1:n] < level[n]))[1])
df <- data.frame(parent = value[parent_idx], child = value, stringsAsFactors = F)
df$parent[is.na(df$parent)] <- ""

list <- ToListExplicit(FromDataFrameNetwork(df), unname = T)
radialNetwork(list)

Here's a tidyverse way of achieving the same...

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(tidyverse)
library(data.tree)
library(networkD3)

data.frame(level, value, stringsAsFactors = F) %>%
  mutate(row = row_number()) %>%
  mutate(level2 = level, value2 = value) %>%
  spread(level2, value2) %>%
  mutate(`0` = "") %>%
  arrange(row) %>%
  fill(-level, -value, -row) %>%
  gather(parent_level, parent, -level, -value, -row) %>%
  filter(parent_level == level - 1) %>%
  arrange(row) %>%
  select(parent, child = value) %>%
  data.tree::FromDataFrameNetwork() %>%
  data.tree::ToListExplicit(unname = TRUE) %>%
  radialNetwork()

and for a bonus, the current dev version of networkD3 (v0.4.9000) has a new treeNetwork function that takes a data frame with nodeId and parentId columns/variables, which eliminates the need for the data.tree fucntions to convert to JSON, so something like this works...

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(tidyverse)
library(networkD3)

data.frame(level, value, stringsAsFactors = F) %>%
  mutate(row = row_number()) %>%
  mutate(level2 = level, value2 = value) %>%
  spread(level2, value2) %>%
  mutate(`0` = "root") %>%
  arrange(row) %>%
  fill(-level, -value, -row) %>%
  gather(parent_level, parent, -level, -value, -row) %>%
  filter(parent_level == level - 1) %>%
  arrange(row) %>%
  select(nodeId = value, parentId = parent) %>%
  rbind(data.frame(nodeId = "root", parentId = NA)) %>% 
  mutate(name = nodeId) %>% 
  treeNetwork(direction = "radial")
Flattop answered 25/12, 2017 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.