Hi,
I am trying to get the click information out of my viz network which is inside a module. I just can't seam to get it to work - here's my code:
library('shiny')
library('dplyr')
library('visNetwork')
library('igraph')
#module UI code
networkdiagramModuleUI <- function(id)
{
ns <- NS(id)
tagList(
fluidRow(style = "margin-left: 30px;margin-bottom: 30px; width=100%; height=900px"
,column(12, uiOutput(ns("click_ui"), height = 800) )
,column(12, visNetworkOutput(ns("Network_plot"), height = 800) )
)
)
}
networkdiagramModule <- function(input,output,session)
{
ns <- session$ns
nodes <- data.frame(id = 1:10,
label = paste("Node", 1:10), # add labels on nodes
group = c("GrA", "GrB"), # add groups on nodes
value = 1:10, # size adding value
shape = c("square", "triangle", "box", "circle", "dot", "star",
"ellipse", "database", "text", "diamond"), # control shape of nodes
title = paste0("<p><b>", 1:10,"</b><br>Node !</p>"), # tooltip (html or character)
color = c("darkred", "grey", "orange", "darkblue", "purple"),# color
shadow = c(FALSE, TRUE, FALSE, TRUE, TRUE))
edges <- data.frame(from = sample(1:10, 8), to = sample(1:10, 8),
label = paste("Edge", 1:8), # add labels on edges
length = c(100,500), # length
arrows = c("to", "from", "middle", "middle;to"), # arrows
dashes = c(TRUE, FALSE), # dashes
title = paste("Edge", 1:8), # tooltip (html or character)
smooth = c(FALSE, TRUE), # smooth
shadow = c(FALSE, TRUE, FALSE, TRUE)) # shadow
output$Network_plot <- renderVisNetwork({
visNetwork(nodes, edges, width = "100%") %>%
visIgraphLayout() %>%
visEvents(click=paste0("function(ng_nodes){
console.log('teste');
Shiny.onInputChange('",ns('got_network_current_node_id'),"',ng_nodes.nodes);}"))%>%
visInteraction(dragNodes = TRUE,dragView = TRUE,navigationButtons = TRUE,multiselect = TRUE, hover=TRUE)
})
vals <- reactiveValues(
vals$myselection <- output$click_ui <- renderUI({
print(input$got_network_current_node_id)
if (is.null(input$got_network_current_node_id) )
{
countn = ''
paste0("No node has been clicked, yet", countn, sep='_')
}
else
{
if (length(input$got_network_current_node_id) == 0)
{
"You have clicked within the visNetwork but not on a node"
}
else
{
nodeid <- input$got_network_current_node_id
#tempnodeid <-unlist(nodeid)
#nodedata = subset(ng_nodes, (ng_nodes$id %in% tempnodeid))
#x<-as.character(nodedata$label)
x <- as.character(nodeid)
print(paste0("Selected Nodes:",paste(unlist(x), collapse = ", ")))
}
}
})
)
return vals
}
# Define UI for application that draws a histogram
ui <- fluidPage(
tabPanel( "NetworkDiagram2", networkdiagramModuleUI("NetworkDiagram"))
)
#server code
server <- function(input, output, session) {
test <- callModule(networkdiagramModule,"NetworkDiagram")
View(test)
}
# Run the application
shinyApp(ui = ui, server = server)
I would appreciate any guidance.
Thanks
Sukhwant