datastorm-open / visNetwork

R package, using vis.js library for network visualization
Other
545 stars 125 forks source link

Obtaining node ID from visOptions(nodesIdSelection = ... ) for later use. #394

Open johnlee4 opened 4 years ago

johnlee4 commented 4 years ago

I need to be able to select nodes from the network and display some information in data table for that node in Shiny. I'm able to do it using

visNetwork(nodes,edges, ...) %>%
visEvents(select = "function(nodes) {
                        Shiny.onInputChange('current_node_id', nodes.nodes);
                        ;}" )

observeEvent(input$current_node_id, {
      # Do stuff
})

and clicking on the node within the network. However, this does not seem to work if I select the node from the drop down list generated by

visOptions(nodesIdSelection = list(enabled = TRUE, ... ))

Are there other options that I should be using?

More detailed implementation:

library(shiny)
library(visNetwork)

server <- function(input, output, session) {
  nodes <- data.frame(id = letters[1:5] )
  edges <- data.frame(from = sample(nodes$id,10, replace=T) , to = sample(nodes$id,10, replace=T) )

  output$tab1plot1 <- renderVisNetwork({
    visNetwork(nodes, edges) %>%
    visOptions( highlightNearest = list(enabled=T,hover=T ),
                       nodesIdSelection = list(enabled=T ) ) %>%
    visEvents(select = "function(nodes) {
                       Shiny.onInputChange('current_node_id', nodes.nodes);
                        ;}" )

  })

  observeEvent(input$current_node_id, {
      df = ...
  })

  output$nodes_data_from_shiny <- renderTable({
     df
  })

}

ui <- fluidPage(
  visNetworkOutput("tab1plot1", height = "400px"),
  tableOutput("nodes_data_from_shiny"),
)

shinyApp(ui = ui, server = server)