daattali / shinycssloaders

⌛ Add loading animations to a Shiny output while it's recalculating
https://daattali.com/shiny/shinycssloaders-demo/
Other
395 stars 45 forks source link

withSpinner with multiple output or a RenderUI #89

Closed yoelii closed 8 months ago

yoelii commented 8 months ago

Hi,

I try to use withSpinner with multiple output computed on server slide inside a renderUI. I try to simplify my problem with two output a datatable and a plot in the same renderUI in this little example :

library(shiny)
library(shinycssloaders)

ui <- fluidPage(
#  withSpinner(tableOutput('data')),
#  withSpinner(plotOutput('plot'))

withSpinner(uiOutput("output_test"))
)

server <- function(input, output) {
  rv <- reactiveValues(a = NULL)

  output$data <- renderTable({
    #Some long calculation here, using Sys.sleep for simplicity
    Sys.sleep(2)
    rv$a <- 1
    head(mtcars)
  })

  output$plot <- renderPlot({
    req(rv$a)
    #Some long calculation here, using Sys.sleep for simplicity
    Sys.sleep(2)
    plot(rnorm(100), rnorm(100))
  })

  output$output_test <- renderUI({
    tagList(
      tableOutput('data'),
      plotOutput('plot')
    )
  })
}

shinyApp(ui, server)

But the spinnerdoes not appear even if the two output is simple.

And of course, it's not works in my true shiny app more complex ( a map created by leaflet and a knobinput in the output hidden if the analysis is not launched).

How can I use withSpinner function with multiple output ?

thanks in advance for your help and advice.

daattali commented 8 months ago

You can wrap each output in a withSpinner(), even if that output is in a render ui. For example:

  output$output_test <- renderUI({
    tagList(
      withSpinner(tableOutput('data')),
      withSpinner(plotOutput('plot'))
    )
  })
yoelii commented 8 months ago

Thanks for your answer. I know I can wrap each output withSpinner() but in my more complex app, my output is a map created by leaflet which I add a sliderTextInput and a knobInput "on" the map. It will be better if the spinner can be for all the output gather.

daattali commented 8 months ago

There are no plans to support withSpinner() to automatically work on multiple outputs.

However, in the dev/GitHub version (it's not on CRAN yet), you can use "Manually triggering the spinner" or the "Full-page spinner" features (you can read about them in the README)