I am trying to use shiny modules to re-use the UI and server code to present off of three different data sets that share the same presentation.
Running into a bit of a challenge dealing with namespace when using javascript based modal popup link creation outside of the UI / server code.
Here is my non-working app code:
library(shiny)
library(shinyBS)
library(DT)
df <- data.frame(id = c('a', 'b', 'c'), value = c(1, 2, 3))
on_click_js = "
Shiny.onInputChange('myLinkName', '%s');
$('#myModal').modal('show')
"
convert_to_link = function(x) {
as.character(tags$a(href = "#", onclick = sprintf(on_click_js, x), x))
}
df$id_linked <- sapply(df$id, convert_to_link)
df <- df[, c('id_linked', 'value')]
mySampleUI <- function(id) {
ns <- NS(id)
fluidPage(
mainPanel(
dataTableOutput(ns('myDT')),
bsModal(id = 'myModal',
title = 'My Modal Title',
trigger = '',
size = 'large',
textOutput(ns('modalDescription'))
),
width = 12
)
)
}
ui <- fluidPage(mySampleUI('myUI'))
myServerFunc <- function(input, output, session, df) {
output$myDT <- DT::renderDataTable({
datatable(df, escape = FALSE, selection='none')
})
output$modalDescription <- renderText({
sprintf('My beautiful %s', input$myLinkName)
})
}
server <- function(input, output) {
callModule(myServerFunc, 'myUI', df)
}
shinyApp(ui = ui, server = server)
A working version would successfully display myLinkName
in the description portion of the modal pop up. The reason this code does not work is because the UI component ID value is created outside of the UI code without the namespace containment. I get that. But, I am not able to figure out how to re-work it so that the name space matches.
Any ideas / options?