2

In Rstudio Shiny, I got some renderDataTable calls that fetch information from a database via RMySQL. Some of the queries may take a few seconds to complete, and I would like to add a "Loading..." message where the table is going to finally render while waiting.

This question is similar to this one: R shiny: display "loading..." message while function is running

But instead of using $('html').hasClass('shiny-busy') as a condition, I would like to somehow condition on the status of the renderDataTable rendering.

Any ideas?

Community
  • 1
  • 1
719016
  • 9,922
  • 20
  • 85
  • 158

3 Answers3

4

Look for the recalculating class instead:

ui <- fluidPage(
    actionButton('reload', 'reload'),
    dataTableOutput('dtable_out'),
    conditionalPanel("$('#dtable_out').hasClass('recalculating')", 
        tags$div('Loading ... ')
    )
)

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

    output$dtable_out <- renderDataTable({
        input$reload
        Sys.sleep(2)
        data.frame(a=1:10, b=letters[1:10])
    })

}

runApp(list(ui=ui, server=server))
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
  • 1
    thanks, I can see the "Loading ..." message appearing and disappearing adequately, but there is still a fraction of time after `recalculating` where the table still isn't displayed. Is that HTML rendering time? – 719016 Aug 07 '14 at 09:00
  • Thank you so much. I've been looking for this exact thing for hours. – wmcnally Mar 09 '17 at 20:14
1

Another way (not too elegant), would be to set as condition something like "NOT the condition that you expect when rendering done".

eg. if you're expecting expectedClass:

conditionalPanel("!$('#dtable_out').hasClass('expectedClass')", tags$div('Loading ... ') )

Worked for me when the above didn't...

lfersol
  • 11
  • 1
1

Package shinycssloaders does the job with some pretty animations: https://github.com/andrewsali/shinycssloaders

enter image description here

shosaco
  • 5,915
  • 1
  • 30
  • 48