1

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)

2 Answers2

3

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"))
    }
  )
}
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
1

We can also start with the button already disabled using shinyjs::disabled()

my_UI <- function(id = "xyz") {
  ns <- NS(id)
  tagList(
    shinyjs::disabled(downloadButton(ns("dl"), "Download"))
  )
}
jpdugo17
  • 6,816
  • 2
  • 11
  • 23