Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny's progress bar just delays progress

I used Shiny's withProgress feature for a long calculation: https://shiny.rstudio.com/reference/shiny/latest/withProgress.html

However, it simply puts the system to sleep and I have to wait for the Sys.sleep(xx) to finish before the calculations are actually undertaken. So I don't see the point of using something that makes the calculation process seem twice as long.

Is there some other progress bar approach that doesn't use Sys.sleep(0.25) so that calculations are actually occurring during the progress status display?

like image 862
Geoff Avatar asked Jul 26 '17 15:07

Geoff


1 Answers

You should not do Sys.sleep(0.25) there, that is just an example to make the progress bar work.

If you have a function that has to sum `10.000.000 random numbers 15 times for example, you could do:

ui <- fluidPage(
  plotOutput("plot")
)

server <- function(input, output) {
  output$plot <- renderPlot({
    withProgress(message = 'Calculation in progress',
                 detail = 'This may take a while...', value = 0, {
                   for (i in 1:15) {
                     incProgress(1/15)
                     sum(runif(10000000,0,1))
                   }
                 })
    plot(cars)
  })
}

shinyApp(ui, server)

Second example:

library(shiny)

ui <- fluidPage(
  plotOutput("plot")
)

test_fun <- function()
{
  for (i in 1:15) {
    incProgress(1/15)
    sum(runif(10000000,0,1))
  }
}

server <- function(input, output) {
  output$plot <- renderPlot({
    withProgress(message = 'Calculation in progress',
                 detail = 'This may take a while...', value = 0, {
                      test_fun()
                 })
    plot(cars)
  })
}

shinyApp(ui, server)
like image 139
Florian Avatar answered Nov 11 '22 17:11

Florian