r语言 - 在打印加载时禁用闪亮按钮



是否可以在加载绘图/反应元素时禁用闪亮的按钮?我知道shinyjs可以禁用和启用输入元素,但我不知道如何设置与加载图/反应元素的连接。该示例基于"单文件闪亮应用"页。我刚刚添加了一个按钮和隔离部分。不基于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)

最新更新