Synchronize shiny's progress bar with plotly's output

853 views
Skip to first unread message

Aya Mahfouz

unread,
Apr 26, 2017, 10:46:06 AM4/26/17
to Shiny - Web Framework for R
Hello,

  I've written some code that works with a data frame and can sometimes
spend sometime to generate a plotly output. I've used shiny's progress bar
but the problem is if plotly takes time to generate the output, the progress
bar will disappear and the application will freeze until the output is displayed.
Is there a way to have the plotly code implemented as a callback for the progress
bar to avoid this situation?

P.S. I can upload a simplified version of the application to github if this will
help with the question.

Thanks for your time and  consideration,

Kind Regards,
Aya Mahfouz





Andrew Sali

unread,
Apr 27, 2017, 1:58:17 AM4/27/17
to Shiny - Web Framework for R
Hi Aya,

I believe the issue is that once renderPlotly has run (R logic to generate the plot data has been finished), the data still needs to be sent from Shiny server to the client (browser) and then the client needs to render/assemble the plot from the data+blueprint received from the server. This can take a substantial amount of time and I believe there is not much that you can do from Shiny side to act on these events.

One workaround that works relatively OK is to put a "loading animation" with a lower z-index than the plot - this will be automatically hidden when the plot contents render. This idea has been introduced by http://deanattali.com/blog/advanced-shiny-tips/#plot-spinner, however a more complete implementation mimicking plotly bars is given here: http://stackoverflow.com/questions/36129522/show-loading-graph-message-in-plotly

What you should be careful with such an implementation is that the "loading animation" will show even if you don't want the plot to show (for example by having some req() or validate() in renderPlotly blocking the execution). In this case you might want to consider moving the loading animation into a dynamic UI with the same kind of blockers enabled.

It's probably most useful to try to put all these aspects into a Shiny module, if you need some help with this, don't hesitate to ask.

Cheers,

Andras

Andrew Sali

unread,
Apr 27, 2017, 5:41:27 AM4/27/17
to Shiny - Web Framework for R
Given how common an issue this seems to be, I created a simple Shiny module that you can use to automatically wrap reactives that return plotly objects in a way that the loading animation is shown whilst the reactive is running / plot is rendered. The loading animation will automatically show / hide in the same way as the plot does, so you don't need to worry about any additional details.

You can see quick installation instructions and a runnable example on Github: https://github.com/andrewsali/plotlyBars

Cheers,

Andras

Aya Mahfouz

unread,
Apr 29, 2017, 12:13:35 PM4/29/17
to Shiny - Web Framework for R

Hello Andrew,

 Thanks for your elaboration and working example! I did use a solution based on the presence
of "shiny-busy" class in the html page per the recommendation of a fellow member in the group.

Kind Regards,
Aya Mahfouz
Reply all
Reply to author
Forward
0 new messages