shinycssloaders icon indicating copy to clipboard operation
shinycssloaders copied to clipboard

Issue with DataTables using withSpinner() showing up if an error occurs instead of clearing

Open Mkranj opened this issue 4 months ago • 0 comments

I have an app that fetches some data and creates a tabular report. While the data is fetching and calculating, the spinner creates a more pleasant UI. However, I've noticed that if an error in the fetch process happens, instead of clearing the DT, it shows up as the last created DT with the error text overlay. Here's an example:

library(shiny)
library(DT)
library(shinycssloaders)

ui <- fluidPage(
  actionButton("trigger_error", "Trigger an error"),
  actionButton("clear_error", "Clear an error"),
  tagList(dataTableOutput("mytable"),
          uiOutput("extra_info")
  ) %>% withSpinner(type = 5, color = "#d71635")
)

server <- function(input, output, session) {
  
  error_active <- reactiveVal(FALSE)
  
  observeEvent(input$trigger_error, {
    error_active(TRUE)
  })
  observeEvent(input$clear_error, {
    error_active(FALSE)
  })
  
  mydata <- reactive({
    
    # pretend computing data takes some time
    Sys.sleep(0.2)
    
    if (error_active()) stop("Triggered user error.")
    
    data.frame(x = 1:10,
               y = 2:11)
  })
  
  output$mytable <- renderDataTable({
    req(mydata())
    
    input$clear_error
    
    datatable(mydata(), escape = FALSE)
  })
  
  output$extra_info <- renderUI({
    req(mydata())
    tagList(
      h2("Extra information")
    )
  })
  
}

shinyApp(ui, server)

To trigger, press Clear error 3 or 4 times (I'm not sure why this number, sometimes it occurs at 2). It should appear as the table with the error overlaid:

Image

In my work this is especially a problem since the table shows the last succesful report, while the system should be creating a different report.

I'm using shiny 1.11.1, DT 0.33, shinycssloaders 1.1.0.

Some things I've noticed:

  • it seems it's important that the DTOutput is in the tagList, and I can't replicate the error if the spinner's JUST on the dataTableOutput
  • renderDataTable()'s expression needs to have some more reactive components, like input$clear_error

Mkranj avatar Sep 09 '25 07:09 Mkranj