Is it possible to disable a button in shiny while a plot / a reactive element is loading? I know shinyjs
can disable and enable input elements, but I don't know how to set up the connection to a loading plot / reactive element. The example is based on the Single-file shiny apps page. I just added a button and the isolated part. Solutions that are not based on shinyjs
are also appreciated :)
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)
Something like this?
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)
In the following way you don't have to set a time. The button is disabled only during the calculation.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With