2

I am trying to implement collapsible network in R based on the blog post more network layouts. However, I am always getting the error of Object not found.

devtools::install_github
devtools::install_github("timelyportfolio/networkD3@feature/d3.chart.layout")
library(htmltools)
library(networkD3)

hc = hclust(dist(mtcars))

treeNetwork( 
   as.treeNetwork(hc, "mtcars")
)

tagList(
  lapply(
    c("tree.cartesian"
      ,"tree.radial"
      ,"cluster.cartesian"
      ,"cluster.radial"
     )
    ,function(chartType){
      hierNetwork(as.treeNetwork(hc), type=chartType, zoomable=T,    collapsible=T)
    }
  )
)
tree.cartesian

EDIT 1

How can we use these graphs for an edges file to build a network? Example:

From   To
 A      B
 A      C
 A      D
 D      L
 L      J
 J      T
 B      O
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
NinjaR
  • 621
  • 6
  • 22
  • Where are you getting "Object not found"? – Marcelo Mar 07 '17 at 06:02
  • @Marcelo - In the console itself – NinjaR Mar 07 '17 at 06:05
  • I meant to ask after which line on the code you are getting the error. – Marcelo Mar 07 '17 at 06:17
  • @Marcelo - After tree.cartesain – NinjaR Mar 07 '17 at 06:18
  • Can you try this command: `hierNetwork( as.treeNetwork(hc), type = "tree.cartesian", zoomable = T, collapsible = T )` It should open the browser with the network chart. – Marcelo Mar 07 '17 at 06:20
  • @Marcelo- Thanks a lot..Its coming up just fine... – NinjaR Mar 07 '17 at 06:22
  • - The code should show all the types of charts but it is not working for me also. You can manually see the other types by replacing `type = "tree.cartesian"` with the other types: "tree.radial", "cluster.cartesian" and "cluster.radial" – Marcelo Mar 07 '17 at 06:26
  • Yes..that should solve the issue...One more thing...is it possible to use this for a data frame which is essentially an edges file. example in the edit of the question. – NinjaR Mar 07 '17 at 06:33

3 Answers3

2

The current official dev version of networkD3 (v0.4.9000 as of 2017.09.02) has a new function treeNetwork that enables collapsible trees.

devtools::install_github("christophergandrud/networkD3")
library(networkD3)

hc <- hclust(dist(mtcars))
treeNetwork(hc)

edges <- read.table(header = T, text = "
From   To
NA     A
A      B
A      C
A      D
D      L
L      J
J      T
B      O
")

edges <- as_treenetdf(edges, cols = c(nodeId = "To", parentId = "From"))
treeNetwork(edges)

It's still in development, so we'd appreciate feedback.

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
0

Yes:

library(networkD3)
to <- c("a","a","a","d","l","j","b") 
from <- c("b","c","d","l","j","t","o")
df <-data.frame(to,from)
simpleNetwork(df)

You should use the official networkD3 package:

install.packages("networkD3")
Marcelo
  • 4,234
  • 1
  • 18
  • 18
0

I'm not sure what the tree.cartesian object is supposed to be in your code, but you're getting the error "Object not found" because the tree.cartesian object is never being created in your code.

Having said that, your code will work if you remove your lapply command from the tagList function. Or more succinctly...

devtools::install_github("timelyportfolio/networkD3@feature/d3.chart.layout")
library(networkD3)

hc = hclust(dist(mtcars))

hierNetwork(as.treeNetwork(hc), type = 'tree.cartesian', zoomable = T, collapsible = T)
# or
hierNetwork(as.treeNetwork(hc), type = 'tree.radial', zoomable = T, collapsible = T)
# or
hierNetwork(as.treeNetwork(hc), type = 'cluster.cartesian', zoomable = T, collapsible = T)
# or
hierNetwork(as.treeNetwork(hc), type = 'cluster.radial', zoomable = T, collapsible = T)

Just to be clear, you are using a branch of networkD3, which (as far as I know) is no longer being developed. There is some current intention to add this feature to the official networkD3 branch, but at the moment (v0.3.1), this feature (collapsible branches) is not currently available.

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56