加载绘图时禁用闪亮的按钮

Disable button in shiny while plot is loading

是否可以在加载绘图/反应元素时禁用 shiny 按钮?我知道 shinyjs 可以禁用和启用输入元素,但我不知道如何设置与加载图/反应元素的连接。该示例基于 Single-file shiny apps page。我只是添加了一个按钮和隔离的部分。不基于 shinyjs 的解决方案也很受欢迎 :)

library(shiny)
server <- function(input, output) {
  output$distPlot <- renderPlot({
    input$button

    isolate(
      hist(rnorm(input$obs), col = 'darkgray', border = 'white')
    )
  })
}

ui <- fluidPage(
  shinyjs::useShinyjs(),
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      actionButton("button", "OK!")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)

是这样的吗?

library(shiny)
library(shinyjs)
server <- function(input, output) {

  PlotData <- eventReactive(input$button,{
    disable("button") 
    Sys.sleep(2)
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
    enable("button") 
  })

  output$distPlot <- renderPlot({
    PlotData()
  })
}

ui <- fluidPage(
  shinyjs::useShinyjs(),
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 1000, value = 2000),
      actionButton("button", "OK!")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)

以下方式可以不用设置时间。该按钮仅在计算期间禁用。

library(shiny)

js <- "
$(document).ready(function() {
  $('#distPlot').on('shiny:recalculating', function() {
    $('button').prop('disabled', true);
    $('button').css('color', 'red');
  });
  $('#distPlot').on('shiny:recalculated', function() {
    $('button').prop('disabled', false);
    $('button').css('color', 'black');
});
});
"

server <- function(input, output) {

  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })
}

ui <- fluidPage(
  tags$head(tags$script(HTML(js))),
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10000, max = 100000, value = 20000),
      actionButton("button", "OK!")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)