visNetwork icon indicating copy to clipboard operation
visNetwork copied to clipboard

Exclusive handling of selectNode and selectEdge events in visEvents

Open Akmazad opened this issue 5 years ago • 1 comments

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)

Akmazad avatar Jul 27 '20 16:07 Akmazad

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

consciousAI avatar Feb 07 '22 18:02 consciousAI