Here is a reproducible example:
library(networkD3)
MyNodes<-data.frame(name= c("A", "B", "C", "D", "E", "F"),
size= c("1","1","1","1","1","1"),
Team= c("Team1", "Team1", "Team1", "Team1", "Team2", "Team2"),
group= c("Group1", "Group1", "Group2", "Group2", "Group1", "Group1"))
MyLinks<-data.frame(source= c("0","2","4"),
target= c("1","3","5"),
value= c("10","50","20"))
forceNetwork(Links = MyLinks, Nodes = MyNodes,
Source = "source",
Target = "target", Value = "value", NodeID = "name",
Nodesize = 'size', radiusCalculation = " Math.sqrt(d.nodesize)+6",
Group = "group", linkWidth = 1, linkDistance = JS("function(d){return d.value * 1}"), opacity = 5, zoom = T, legend = T, bounded = T)
What I want to do is letting the user see only the plots of different Teams as in my example via a selectInput or similar.
I had come across the same issue when I was using visNetwork and managed to solve it by using this trick:
MyNodes[MyNodes$"Team"=="Team2",]
and the way of using selectInput as below would work perfectly with that:
library(shiny)
library(networkD3)
server <- function(input, output) {
output$force <- renderForceNetwork({
forceNetwork(Links = MyLinks, Nodes = MyNodes[MyNodes$"Team"==input$TeamSelect,],
Source = "source",
Target = "target", Value = "value", NodeID = "name",
Nodesize = 'size', radiusCalculation = " Math.sqrt(d.nodesize)+6",
Group = "group", linkWidth = 1, linkDistance = JS("function(d){return d.value * 1}"), opacity = 5, zoom = T, legend = T, bounded = T)
})}
ui <- fluidPage(
selectInput("TeamSelect", "Choose a Team:", MyNodes$Team, selectize=TRUE),
forceNetworkOutput("force"))
shinyApp(ui = ui, server = server)
However with networkD3, I guess something goes wrong with the interpretation of the index order of the nodes following the subset and as you can also see, I get my selectInput with my Teams but when I choose one, it returns an empty plot.
I also tried to shape-shift the solution with reactive here for my case, but it didn't work either:
Create shiny app with sankey diagram that reacts to selectinput
Is it technically not possible to do this in networkD3, or how close was I to the solution?
Thanks!