Search code examples
rshinyshinydashboardshinymodules

disable does not work for downloadButton when using uiOutput/renderUI


I have a simple ui/server modules. When I try using uiOutput/renderUI, the disable/enable function does not work. But, if I call ui module directly in the ui, it works fine.

library(shiny)
library(shinyjs)
library(shinydashboard)

my_UI <- function(id = "xyz") {
  ns <- NS(id)
  tagList(
    downloadButton(ns("dl"), "Download")
  )
}

my_Server <- function(id = "xyz") {
  moduleServer(id,
               function(input, output, session) {
                 disable("dl")
               }
  )
}

ui <- dashboardPage(
  dashboardHeader(title = "test"),
  dashboardSidebar(disable = TRUE),
  dashboardBody(
    useShinyjs(),
    
    uiOutput("app_ui")
    # my_UI()
    
  )
)

server <- function(input, output, session) {
  
  output$app_ui <- renderUI({
    my_UI()
  })
  
  my_Server()
  
}

shinyApp(ui, server)

Solution

  • That's because the download button is not rendered yet when disable is executed. You can use shinyjs::delay to delay the execution. Actually this works with a delay of 0ms, because this function also puts the expression it executes in a queue.

    my_Server <- function(id = "xyz") {
      moduleServer(
        id,
        function(input, output, session) {
          delay(0, disable("dl"))
        }
      )
    }