Monday, 30 January 2023

R/Javascript: Collapsing and Expanding Networks

I am working with the R programming language.

I have the following graph network data:

library(igraph)
library(visNetwork)

from <- c("Boss", "TeamA", "TeamA", "TeamA", "SubteamA1", "SubteamA1", "SubteamA1", "SubteamA2", "SubteamA2", "SubteamA2", "SubteamA3", "SubteamA3", "SubteamA3")
to <- c("TeamA", "SubteamA1", "SubteamA2", "SubteamA3", "employee1", "employee2", "employee3", "employee4", "employee5", "employee6", "employee7", "employee8", "employee9")
a1 = data_frame <- data.frame(from, to)


from <- c("Boss", "TeamB", "TeamB", "TeamB", "SubteamB1", "SubteamB1", "SubteamB1", "SubteamB2", "SubteamB2", "SubteamB2", "SubteamB3", "SubteamB3", "SubteamB3")
to <- c("TeamB", "SubteamB1", "SubteamB2", "SubteamB3", "employee10", "employee11", "employee12", "employee13", "employee14", "employee15", "employee16", "employee17", "employee18")
a2 = data_frame <- data.frame(from, to)


final = rbind(a1, a2)

I then made it into a graph network and visualized it:

# Convert the data frame to an igraph object
g <- graph_from_data_frame(final, directed=FALSE)

# Plot the graph
plot(g)

# Optional visualization
visIgraph(g)

visIgraph(g) %>%
  visHierarchicalLayout(direction = "LR") %>%
  visInteraction(navigation = "zoom") %>%
  visInteraction(navigation = "drag") %>%
  visOptions(selectedBy = "to", 
             highlightNearest = TRUE, 
             nodesIdSelection = TRUE) 

enter image description here

My Question: I have been trying to find if there some way such that when you run the graph, it only shows one node on the screen (boss node) - and when you click on the boss node, it expands into 3 nodes (boss, team a, team b), and if you click on "team a", it expands into sub teams ... but if you double click, it collapse back to the previous layer.

The closest thing I could find to this is here: https://github.com/datastorm-open/visNetwork/issues/307

But is there some easier way to do this in R/javascript? In the end, the final output should be a (standalone) HTML file that can be viewed offline.

Thanks!

Note:



from R/Javascript: Collapsing and Expanding Networks

No comments:

Post a Comment