When working on a larger app that has Shiny modules that include modules themselves, I've noticed that I can't put a downloadbutton in a renderUI call without it being disabled. Here's an example:
library(shiny)
# Module - top level
topLevel_UI <- function(id) {
ns <- NS(id)
tagList(
dl_UI(ns("my_module"))
)
}
topLevel_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
dl_Server("my_module")
}
)
}
# Module - lower level
dl_UI <- function(id) {
ns <- NS(id)
tagList(
uiOutput(ns("download_button"))
)
}
dl_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
ns <- NS(id)
output$download_button <- renderUI({
downloadButton(ns("download"), label = "Download file")
})
output$download <- downloadHandler(
filename = function() {"test.csv"},
content = function(file) {
write.csv(iris, file)
}
)
}
)
}
# Main app
ui <- fluidPage(
h1("Application"),
topLevel_UI("app")
# Including the lower level directly works
# , dl_UI("low")
)
server <- function(input, output, session) {
topLevel_Server("app")
# Including the lower level directly works
# , dl_Server("low")
}
shinyApp(ui, server)
When running the app, the button is disabled and cannot be clicked. If the module wasn't nested in topLevel module but called directly in main shiny app it works as intended (the comments in the code show this type of calling the module).
How can I fix it? It's probably due to namespacing issues.
I need the downloadButton to be in a renderUI call so I can have more logic before it.