HenrikBengtsson/progressr

withProgressShiny stops with wrong error message when function contains an error message

julienleroux5 opened this issue · 0 comments

Hi

I am trying to run a Shiny app with both withProgressShiny for a progressbar and shinyCatch from the spsCompspackage to catch errors and messages but not interrupt the app.

When the long operation has no errors, messages are displayed, the progress finishes and all is ok. When the long operation has an error, e.g. custom stop() message inside the function, the custom error is not displayed and instead the withProgressShiny function displays an "error in if, zero length argument" error, which makes shinyCatch and the app stop.

I simplified the problem with this MRE:

library(shiny)
library(spsComps)
library(progressr)

ui <- fluidPage(

    # Application title
    titlePanel("Title"),

    sidebarLayout(
      sidebarPanel(
        actionButton("long_operation", label = "Launch long operation with progress")
      ),
      mainPanel(
        h3("Title"),
        textOutput("text")
      )
    )
)
server <- function(input, output) {

  observeEvent(input$long_operation, {
      
      spsComps::shinyCatch({
        X <- 1:15
        withProgressShiny(message = "In progress...", {
        message("starting")
          p <- progressor(along = X)
          y <- lapply(X, FUN=function(x) {
            Sys.sleep(0.2)
            p(sprintf("x=%d", x))
            
          })
          stop("error: stopping operation") # custom error from the function. if commented, no problem.
        message("finished")
        })
      },
      prefix = "", blocking_level = "error"
      # if blocking_level = "error": stops and finishes app execution. 
      # if blocking_level = "none": no interruption but the following message is displayed. 
      )
    output$text <- renderPrint("You shouldn't see me")      
    })

}
shinyApp(ui = ui, server = server)

The error can also be obtained without shinyCatch.
I tried to track down the origin of the error and apparently it's from the if (config$max_steps == 0) parts in the handler_shiny() function.
My understanding of how progressr works (and this config object) is too limited to understand how to solve this.