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

Add binding to `shiny:recalculating` event #63

Closed vhorvath closed 1 year ago

vhorvath commented 3 years ago

Today I ran into the following issue with withSpinner().

I have modal with tabs in it. When a tab is selected the content must be refreshed, which is a slow process. I wanted to use withSpinner() to wrap a DT::dataTableOutput().

It took me quite a while to find out that this does not work, because the backend triggers an event that the spinner is not receptive to, here is the message log:

SEND {"recalculating":{"name":"item_id","status":"recalculating"}}

Spinner listens to shiny:outputinvalidated, but that is never triggered, at least in Shiny 1.5.0, so it remains hidden.

When I paused the JS execution in Chrome and manually added an identical listener to shiny:recalculating it suddenly started working.

Perhaps listening to this event in addition to the original one could be helpful for others as well. Very minor difference shown below.

$(document).on('shiny:outputinvalidated shiny:recalculating', function(event) {
  var id = event.target.id;
  if (id === undefined) {
    return;
  }
  output_states[id] = 0;
  update_spinner(id);
});

If this makes sense, I can create a PR.

Also, if someone is interested in using this early, you can include the following file (extracted as spinner-patch.js) at the beginning of your Shiny app as part of the header somewhere.

tags$head(singleton(includeScript("spinner-patch.js")))

spinner-patch.zip

daattali commented 2 years ago

Sorry for the very slow reply -- could you please share a minimal code example? I'm not sure if this is a bug or a correct behaviour from shiny, because reading the docs for the javascript events seems to suggest that recalculating should follow outputinvalidated. With a complete example I'd like to see what's happening and if the JS events are firing correctlly, because I'm not sure why in such a specific case an event isn't fired

daattali commented 1 year ago

Closing due to no reprex