I need to identify which node in a DiagrammeR output has been clicked in a Shiny app. Following this post, I can get the information I need when the output is not produced by a module. But in a module (my real use case), the same logic seems not to work. I can't see why, but I did notice that the DiagrammeR nodes appear not to respect the module's namespace (that is, the first node's id is node1
rather than <namespace>-node1
).
What am I doing wrong, or is this a bug in DiagrammeR?
Here's my sample code.
library(shiny)
library(DiagrammeR)
library(shinyjs)
texts <- c("Clicked on A", "Clicked on B")
moduleUI <- function(id) {
ns <- NS(id)
tagList(uiOutput(ns("tooltip")), grVizOutput(ns("tree")))
}
moduleController <- function(input, output, session) {
ns <- session$ns
jsCode <- paste0("Shiny.onInputChange('", ns("clickedElemNr"), "',", 1:2, ")")
observeEvent(input$clickedElemNr, {
print(ns("observeEvent[clickedElemNr]"))
output$tooltip <- renderUI({
textInput(inputId=ns("x"), label="x", value=texts[input$clickedElemNr])
})
})
observe({
output$tooltip <- renderUI({textInput(inputId=ns("x"), label="x", value="Click an element")})
for (i in 1:length(jsCode)) {
local({
jsToAdd <- jsCode[i]
shinyjs::onclick(ns(paste0("node", i)), runjs(jsToAdd))
})
}
})
output$tree <- renderGrViz({
grViz("digraph test {A; B; A -> B;}")
})
}
ui <- fluidPage(
useShinyjs(),
column(width=4, wellPanel("No module", uiOutput("tooltip"), grVizOutput("tree"))),
column(width=4, wellPanel("Module 1", moduleUI("mod1")))
)
server <- function(input, output) {
jsCode <- paste0("Shiny.onInputChange('clickedElemNr',", 1:2, ")")
callModule(moduleController, "mod1")
observeEvent(input$clickedElemNr, {
print("observeEvent[clickedElemNr]")
output$tooltip <- renderUI({
textInput(inputId="x", label="x", value=texts[input$clickedElemNr])
})
})
observe({
output$tooltip <- renderUI({textInput(inputId="x", label="x", value="Click an element")})
for (i in 1:length(jsCode)) {
local({
jsToAdd <- jsCode[i]
shinyjs::onclick(paste0("node", i), runjs(jsToAdd))
})
}
})
output$tree <- renderGrViz({
grViz("digraph test {A; B; A -> B;}")
})
}
shinyApp(ui = ui, server = server)