datastorm-open / visNetwork

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

Exclusive handling of selectNode and selectEdge events in visEvents #385

Open Akmazad opened 4 years ago

Akmazad commented 4 years ago

Hi guys,

Not sure if its a duplicate but, when I try to select any node with selectNode parameter in visEvents function, seems like it fires the selectEdge events too. All I want to have them as separate events (i.e. not affecting each other), and perform individual actions accordingly, if possible.

Any suggestions or tips would be greatly appreciated.

Minimal code to reproduce the above case:

require(visNetwork)
require(shiny)
require(dplyr)

nodes <- data.frame(id = 1:15, label = paste("Label", 1:15),
                    group = sample(LETTERS[1:3], 15, replace = TRUE))

edges <- data.frame(from = trunc(runif(15)*(15-1))+1,
                    to = trunc(runif(15)*(15-1))+1)

server <- function(input, output, session) {
  output$network <- renderVisNetwork({
    visNetwork(nodes, edges, 
               height = "100%", width = "100%",
               main = "") %>%
      visEvents(selectNode = "function(nodes){
                  Shiny.onInputChange('click', nodes.nodes[0]);
                  }"
      ) %>%
      visEvents(selectEdge = "function(properties) {
                                    Shiny.onInputChange('current_edges_selection', properties.edges);
                                  }")
  })

  output$shiny_return <- renderPrint({
    input$click
  })

  output$shiny_return2 <- renderPrint({
    input$current_edges_selection
  })
}

ui <- fluidPage(
  visNetworkOutput("network"), 
  verbatimTextOutput("shiny_return"),
  verbatimTextOutput("shiny_return2")  
)

shiny::shinyApp(ui = ui, server = server)
consciousAI commented 2 years ago

Any updates on this one? I am having the same issue.