I am having a hard time figuring out why in VisNetwork, the nodesIdSelection
argument in VisOptions
does not behave similarly to the selectNode
argument in visEvents
.
Basically, when I click on any node, the R console returns as expected "The selected node ID is: X" but when I select the node from the dropdown list (generated with visOptions) then the message is not issued in the console.
Here is a reproducible example:
library(shiny)
library(visNetwork)
ui <- fluidPage(
visNetworkOutput('network')
)
server <- function(input, output, session) {
getDiagramPlot <- function(nodes, edges){
v <- visNetwork(
nodes,
edges
) %>%
visPhysics(stabilization = TRUE, enabled = TRUE) %>%
visOptions(highlightNearest = list(enabled = T, degree = 1, hover = F),
nodesIdSelection = list(enabled = TRUE, main = "Select ID"),
autoResize = TRUE, collapse = FALSE) %>%
visEdges(color = list(highlight = "red")) %>% # The colour of the edge linking nodes
visLayout(improvedLayout = TRUE) %>%
visEdges(arrows = edges$arrows) %>%
visInteraction(multiselect = F) %>%
visEvents(selectNode = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}")
return(v)
}
testFunction <- function(node_id){
print(paste("The selected node ID is:", node_id))
}
nodes <- data.frame(id = 1:3, label = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
output$network <- renderVisNetwork(
getDiagramPlot(nodes, edges)
)
observeEvent(input$current_node_id,{
testFunction(input$current_node_id)
})
}
shinyApp(ui, server)
Is there a way to pass the same javascript code in visEvents
to nodesIdSelection
?
Best,
C.
I tried to pass the javascript code from visEvents:
"function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}"
to the style
HTML argument (in nodesIdSelection
) but this had not effect.