Click events for VisNetwork with Shiny
Asked Answered
W

1

5

I have built my network with the visNetwork package in Shiny. I would like to click on a node and then display the information about the node from a dataframe. I have been able to do this for scatterplots with the click and nearpoint functions, such as the ones in the Shiny example shown here: http://shiny.rstudio.com/gallery/plot-interaction-selecting-points.html.

For my network, I have tried:

server <- function(input, output) {
output$network <- renderVisNetwork({
visNetwork(my.nodes, my.edges, 
           height = "100%", width = "100%",
           main = "") %>%
visEvents(hoverNode = "function(nodes){
            Shiny.onInputChange('current_node_id',nodes);
            ;}",
            click = "function(){
            Shiny.onInputChange('click',{node: current_node_id});
            ;}"
  )
})

output$shiny_return <- renderPrint({
if(!is.null(input$current_node_id)){
nearPoints(node.data,click$node, addDist = TRUE )    
}
})

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

But, I get an error saying "click object not found"

Thank you for your help.

Whiting answered 7/10, 2016 at 11:45 Comment(0)
H
9

Differents points :

  • your javascript event click is wrong. Don't know about current_node_id and must be call in shiny with input$click and not click$node
  • nearPoints is only for plotOuput. Can't use with javascript / htmlwidgets functions.

To enabled this kind of function with visNetwork, I've just add a new function visNearestNodes in the latest dev vervion. This is a simple example :

# install dev version
devtools::install_github("datastorm-open/visNetwork")

require(visNetwork)
require(shiny)

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(click = "function(nodes){
                  Shiny.onInputChange('click', nodes.nodes[0]);
                  ;}"
      )
  })

  output$shiny_return <- renderPrint({
    visNetworkProxy("network") %>%
      visNearestNodes(target = input$click)
  })
}

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

shiny::shinyApp(ui = ui, server = server)
Hagerty answered 11/10, 2016 at 8:58 Comment(5)
Hi again. I am trying this on new graph and it will not print the neighbours. I am getting an error: Warning: Error in rbind.data.frame: invalid list argument: all variables should have the same length. I am using the same code and it works for the example you provided.Whiting
visNearestNodes is really young. I've just add some control and change the data reconstruction. So can you try with the latest github version please ? And if still an error, send me little data example to reproduce and fix that ?Hagerty
Can it only show 5 neighbours? Currently, I cannot get information on more than 5. If that's the case, that's fine. This is still fantastic!Whiting
you can use maxpoints arguments in visNearestNodes. Defaut setting to maxpoints = 5Hagerty
Thanks! Works like a charm.Whiting

© 2022 - 2024 — McMap. All rights reserved.