0

The transitions of my Shiny app are very long when I change a numericInput value by clicking on the button "quickly" several times in a row .

How can I stop the previous code from running once a new numericInput has changed ? I don't know if I explained my problem clearly. Is it possible to put a GIF in a StackOverflow post ?

Try to play with the button to understand my problem enter image description here

# USER INTERFACE 
ui <-  fluidPage(
  sidebarLayout(
    sidebarPanel(numericInput("sd", p("sd"), value = 0.1, step = 0.1)),
    mainPanel(plotOutput("plot"))
  )
)

# SERVER 
server <- function(input, output) {
  
  output$plot<- renderPlot({
    x <- seq(-1, 1, length.out = 50000)
    plot(x, x + rnorm(50000, sd = input$sd), ylab =  "y")
  })
  
}

shinyApp(ui = ui, server = server)
Julien
  • 1,613
  • 1
  • 10
  • 26

1 Answers1

1

You can’t cancel a computation once it’s started without setting up some sort of subprocess processing. However, even that would not help in this case, because the time-consuming operation is the actual graphics rendering. You’d need a custom equivalent to renderPlot() to handle that.

Likely the best you can do here is to debounce() the input, so that you won’t start plotting until the input value has settled for some amount of time:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(numericInput("sd", p("sd"), value = 0.1, step = 0.1)),
    mainPanel(plotOutput("plot"))
  )
)

server <- function(input, output) {
  input_sd <- debounce(reactive(input$sd), 400)
  output$plot <- renderPlot({
    x <- seq(-1, 1, length.out = 50000)
    plot(x, x + rnorm(50000, sd = input_sd()), ylab = "y")
  })
}

shinyApp(ui = ui, server = server)

Or if changing inputs too quickly is still a problem even when debounced, accept that and use a submit button to explicitly trigger the plotting.

Mikko Marttila
  • 10,972
  • 18
  • 31
  • 1
    Too bad that there is not an easy solution. I'll try to play with the parameter of `debounce` then – Julien May 06 '22 at 07:09