JohnCoene / waiter

🕰️ Loading screens for Shiny
https://waiter.john-coene.com/
Other
495 stars 25 forks source link

withProgressWaitress and incProgressWaitress not responding to max #123

Open tvedebrink opened 2 years ago

tvedebrink commented 2 years ago

Hi John

I've been trying to use your intuitive package for showing a progress on a slow computation. After finishing the computations, the user should be able to download the results. However, it seems to me that the progress bar isn't reacting to my specified max (greater than 100) number of calculations. The example below shows an example, where the bar reaches 100% before the process reaches N. I've been trying to fiddle around with various hacks involving manual setting of the increment (to 1/N) and also providing the session to the incProgressWaitress function. But nothing really works as intended. I hope you can find my bug??

Best wishes, Torben

library(shiny)
library(shinyjs)
library(waiter)

ui <- fluidPage(
  useWaitress(),
  useShinyjs(),
  br(),
  actionButton(
    "load",
    "Load stuff"
  ),
  shinyjs::disabled(downloadButton(
    "dwnload",
    "Download"
  ))
)

hep_hep <- function(n, .session){
  incProgressWaitress(100/n, session = .session)
  Sys.sleep(10/n)
}

hep_hep_ <- function(n){
  incProgressWaitress(1)
  Sys.sleep(10/n)
}

hep_hep__ <- function(n, .session){
  incProgressWaitress(1, session = .session)
  Sys.sleep(10/n)
}

N <- 150

server <- function(input, output, session) {

  output$dwnload <- downloadHandler(
    filename = function() {
      paste('data-', Sys.Date(), '.csv', sep='')
    },
    content = function(con) {
      write.csv(hit_me, con)
    }
  )

  observeEvent(input$load,{
    withProgressWaitress({
      for (i in 1:N) {
        hep_hep(N, .session = session)
        print(i)
      }
    }, selector = "#dwnload", max = N, theme = "overlay-percent")
    shinyjs::enable("dwnload")
    hit_me <<- mtcars
  })
}

shinyApp(ui, server)
tvedebrink commented 2 years ago

A sort of solution - rather than invoking the incProgressWaiter with an adjusted amount it is only activated when the increment is bigger than a 1% of the total calculation:

library(shiny)
library(shinyjs)
library(waiter)

ui <- fluidPage(
  useWaitress(),
  useShinyjs(),
  br(),
  actionButton("load", "Load stuff"),
  shinyjs::disabled(downloadButton("dwnload","Download"))
)

hep_hep_fix <- function(trick = FALSE, n, .session){
  if(trick) incProgressWaitress(value = 1, session = .session)
  Sys.sleep(10/n)
}

N <- 150

server <- function(input, output, session) {
  output$dwnload <- downloadHandler(
    filename = function() {
      paste('data-', Sys.Date(), '.csv', sep='')
    },
    content = function(con) {
      write.csv(hit_me, con)
    }
  )

  observeEvent(input$load,{
    withProgressWaitress({
      for (i in 1:N) {
        hep_hep_fix(trick = floor(i*100/N) > floor((i-1)*100/N), n = N, .session = session)
      }
    }, selector = "#dwnload", max = N, theme = "overlay-percent")
    shinyjs::enable("dwnload")
    hit_me <<- mtcars
  })
}

shinyApp(ui, server)