【问题标题】:disable does not work for downloadButton when using uiOutput/renderUI使用 uiOutput/renderUI 时禁用对 downloadButton 不起作用
【发布时间】:2023-01-30 01:29:25
【问题描述】:

我有一个简单的 ui/server 模块。当我尝试使用 uiOutput/renderUI 时,禁用/启用功能不起作用。但是,如果我直接在 ui 中调用 ui 模块,它就可以正常工作。

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)

【问题讨论】:

    标签: r shiny shinydashboard shinymodules


    【解决方案1】:

    那是因为执行disable时下载按钮还没有渲染。您可以使用shinyjs::delay 来延迟执行。实际上这会延迟 0 毫秒,因为此函数还将其执行的表达式放入队列中。

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

    【讨论】:

      【解决方案2】:

      我们也可以使用shinyjs::disabled()从已经禁用的按钮开始

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

      【讨论】:

        猜你喜欢
        • 2020-09-29
        • 2019-11-15
        • 2016-11-01
        • 1970-01-01
        • 2019-06-16
        • 2021-11-21
        • 1970-01-01
        • 2018-09-18
        • 1970-01-01
        相关资源
        最近更新 更多