Tengo una pequeña aplicación brillante que ejecuta una operación que a veces es instantánea ya veces tarda unos segundos. En el último caso, quiero mostrar un modal. Lo siguiente funciona bastante bien si la operación lleva mucho tiempo; pero también parpadea el modal durante unos ms en la operación instantánea; que es bastante feo.
library(shiny) ui <- fluidPage( # In the real app, there is only one operation that can either be fast or slow. # The buttons in this example represent both possible scenarios. actionButton("slowButton", "save slow"), actionButton("fastButton", "save fast") ) server <- function(input, output, session) { observeEvent(input$slowButton, { showModal(modalDialog("Saving...")) Sys.sleep(3) removeModal() }) observeEvent(input$fastButton, { # The modal should be suppressed here, because the operation is fast enough. showModal(modalDialog("Saving...")) Sys.sleep(0) # In reality, we do not know how long this takes. removeModal() }) } shinyApp(ui, server)¿Hay alguna manera de agregar un retraso para mostrar el modal solo si la operación lleva más de, digamos, medio segundo?
Aquí hay un ejemplo de lo que he esbozado en los comentarios. Sin embargo, para un tiempo de retraso de solo 0,5 s para mostrar el modal, la sobrecarga para crear un proceso en segundo plano podría ser demasiado.
library(shiny) library(shinyjs) library(callr) ui <- fluidPage( useShinyjs(), actionButton("saveButton", "save"), ) server <- function(input, output, session) { rv <- reactiveValues(bg_process = NULL, retry_count = 0) observeEvent(input$saveButton, { disable("saveButton") unknown_duration <- round(runif(1L, max = 2L), digits = 1) print(paste0(Sys.time(), " - unknown process duration: ", unknown_duration, "s")) rv$bg_process <- r_bg( func = function(duration){ Sys.sleep(duration) return("result") }, args = list(duration = unknown_duration)) }) observe({ req(rv$bg_process) if (rv$bg_process$poll_io(0)["process"] == "ready") { print(paste(Sys.time(), "-", rv$bg_process$get_result())) rv$retry_count <- 0 enable("saveButton") removeModal() if(rv$bg_process$is_alive() == FALSE){ rv$bg_process <- NULL # reset } } else { invalidateLater(1000) if(isolate({rv$retry_count}) == 3L){ print(paste(Sys.time(), "- showModal")) showModal(modalDialog("Saving...")) } else { print(paste(Sys.time(), "- waiting")) } } isolate({rv$retry_count <- rv$retry_count + 1}) }) } shinyApp(ui, server)