I used the example given in this question but I would like to adapt it to display the data of the node that has been selected (not all the node but only this one) and also not use the action button so that the data are displaid as soon as I click on the node.
I have tried many solution without success.
When I create my graph, I upload a CSV file and associate other parameters to the nodes (size, title ...). Would it be possible to display these parameters also when I select a node :
nodes$company_name
nodes$company_postcode
nodes$amount.size
...
Here is the code I used to start with, kindly given by @xclotet
require(shiny)
require(visNetwork)
server <- function(input, output, session) {
nodes <- data.frame(id = 1:3,
name = c("first", "second", "third"),
extra = c("info1", "info2", "info3"))
edges <- data.frame(from = c(1,2), to = c(1,3), id= 1:2)
output$network_proxy <- renderVisNetwork({
visNetwork(nodes, edges)
})
output$nodes_data_from_shiny <- renderDataTable({
if(!is.null(input$network_proxy_nodes)){
info <- data.frame(matrix(unlist(input$network_proxy_nodes), ncol = dim(nodes)[1],
byrow=T),stringsAsFactors=FALSE)
colnames(info) <- colnames(nodes)
info
}
})
observeEvent(input$getNodes,{
visNetworkProxy("network_proxy") %>%
visGetNodes()
})
}
ui <- fluidPage(
visNetworkOutput("network_proxy", height = "400px"),
dataTableOutput("nodes_data_from_shiny"),
actionButton("getNodes", "Nodes")
)
shinyApp(ui = ui, server = server)
Offering up another suggestion, as the one above did not seem to work as elegantly:
library(shiny)
library(visNetwork)
library(DT)
server <- function(input, output, session) {
nodes <- data.frame(id = 1:3,
name = c("first", "second", "third"),
extra = c("info1", "info2", "info3"))
edges <- data.frame(from = c(1,2), to = c(1,3), id = 1:2)
output$network_proxy <- renderVisNetwork({
visNetwork(nodes, edges) %>%
visEvents(select = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}")
})
myNode <- reactiveValues(selected = '')
observeEvent(input$current_node_id, {
myNode$selected <<- input$current_node_id
})
output$table <- renderDataTable({
nodes[which(myNode$selected == nodes$id),]
})
output$dt_UI <- renderUI({
if(nrow(nodes[which(myNode$selected == nodes$id),])!=0){
dataTableOutput('table')
} else{}
})
}
ui <- fluidPage(
visNetworkOutput("network_proxy", height = "400px"),
dataTableOutput("nodes_data_from_shiny"),
uiOutput('dt_UI')
)
shinyApp(ui = ui, server = server)
To display data of the node selected, you can adapt the example given in visNetwork Shiny webpage. In that example, hoverNode option of visEvents is used to get information of the hovered node.
To get the selected node id one can use:
visEvents(select = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}")
This function sets the id of the node (nodes.nodes) to input$current_node_id. Then, you can use this information to display only the information corresponding to that node (by subsetting the data.frame).
Below, the example provided adapted to answer the question:
require(shiny)
require(visNetwork)
server <- function(input, output, session) {
nodes <- data.frame(id = 1:3,
name = c("first", "second", "third"),
extra = c("info1", "info2", "info3"))
edges <- data.frame(from = c(1,2), to = c(1,3), id = 1:2)
output$network_proxy <- renderVisNetwork({
visNetwork(nodes, edges) %>%
visEvents(select = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}")
})
output$nodes_data_from_shiny <- renderDataTable( {
if (!is.null(input$current_node_id) && !is.null(input$network_proxy_nodes)) {
info <- data.frame(matrix(unlist(input$network_proxy_nodes),
ncol = dim(nodes)[1], byrow = T),
stringsAsFactors = FALSE)
colnames(info) <- colnames(nodes)
info[info$id == input$current_node_id, ]
}
})
observeEvent(input$current_node_id, {
visNetworkProxy("network_proxy") %>%
visGetNodes()
})
}
ui <- fluidPage(
visNetworkOutput("network_proxy", height = "400px"),
dataTableOutput("nodes_data_from_shiny"),
actionButton("getNodes", "Nodes")
)
shinyApp(ui = ui, server = server)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With