【问题标题】:Disabling buttons in Shiny在 Shiny 中禁用按钮
【发布时间】:2016-11-15 23:13:39
【问题描述】:

我正在编写一些闪亮的代码,用户将在应用程序中输入一些输入,然后单击一个操作按钮。操作按钮会触发一堆需要很长时间才能运行的模拟,所以我希望在单击操作按钮后将其禁用,以便用户在运行模拟之前无法继续单击它。我遇到了shinyjs::enableshinyjs::disable 函数,但一直很难使用它们。这是我的服务器代码:

output$button1= renderUI({
        if(input$Button1 > 0)  {
          shinyjs::disable("Button1")
                tableOutput("table")
          shinyjs::enable("Button1")}
  })

但是,当我使用此代码并单击操作按钮时,没有任何反应。即,操作按钮不会变灰,也不会生成表格。但是,当我删除shinyjs::enable() 命令时,即

    output$button1= renderUI({
            if(input$Button1 > 0)  {
              shinyjs::disable("Button1")
                    tableOutput("table")
}
      })

首先生成表格,然后按钮变为灰色,但我希望按钮变为灰色,然后表格自行生成。

我在这里做错了什么?


这是我根据 Geovany 的建议更新的代码,但它仍然不适合我

Button1Ready <- reactiveValues(ok = FALSE)

        observeEvent(input$Button1,  {
          shinyjs::disable("Button1")
          RunButton1Ready$ok <- FALSE
          RunButton1Ready$ok <- TRUE
  })

output$SumUI1= renderUI({
        if(Button1Ready$ok){
          tableOutput("table")
          shinyjs::enable("Button1")
        }
})

我还有什么需要澄清的地方:

output$table <- renderTable({

#My code....

)}

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我认为您在同一个反应函数中使用了shinyjs::disableshinyjs::enable。你只会看到最后一个效果。我建议您将 disable/enable 拆分为不同的反应函数,并使用额外的反应变量来控制按钮的重新激活。

    我不知道你的代码到底有多准确,但在下面的代码中说明了主要思想。

    library(shiny)
    library(shinyjs)
    
    
    ui <- fluidPage(
      shinyjs::useShinyjs(),
      sidebarLayout(
        sidebarPanel(
          actionButton("Button1", "Run"),
          shinyjs::hidden(p(id = "text1", "Processing..."))
        ),
        mainPanel(
           plotOutput("plot")
        )
      )
    )
    
    server <- function(input, output) {
    
      plotReady <- reactiveValues(ok = FALSE)
    
      observeEvent(input$Button1, {
        shinyjs::disable("Button1")
        shinyjs::show("text1")
        plotReady$ok <- FALSE
        # do some cool and complex stuff
        Sys.sleep(2)
        plotReady$ok <- TRUE
      })  
    
      output$plot <-renderPlot({
        if (plotReady$ok) {
          shinyjs::enable("Button1")
          shinyjs::hide("text1")
          hist(rnorm(100, 4, 1),breaks = 50)
        }
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 嗨 Geovany,我根据您的建议更新了我的问题。仍然对我不起作用。介意看一下,看看是否只是一个简单的错误?
    • 如果没有问题的最小工作示例,很难知道问题出在哪里。请提供一些任何人都可以执行的工作代码。
    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多