Delayed execution in R Shiny app

1,787 views
Skip to first unread message

Rohith V

unread,
Dec 10, 2013, 4:33:10 AM12/10/13
to shiny-...@googlegroups.com

Is it possible have some parts of RShiny app execute in a delayed fashion much like the Delayed start in Windows Services?

Let me elaborate.

I have a shiny app with tabs. Each tab have a bunch of radio buttons on the sidebarPanel. Clicking on each radio button brings up a report. My set up is as simple as this.

However when I load the app every time and when the first tab is auto rendered, all reports associated with all radio buttons under this tab is executed and then the first radio button is selected and its correlating report is displayed. This whole process takes about 10-11 seconds which I want to bring down.

During server start, I simply read my myData.RData file in global.R. So all data is pre-fetched (and I am assuming kept in memory) during server start. What happens when the tab is brought to focus is that the data.frames from myData.RData are read and a series of ggplots (renderPlot) and tables (renderText) are called.

Is there a way I can render the first report within few seconds and then proceed to executing other ggplots and tables? I did go thru reactivity conductors and isolations but couldn't figure what solution fits my problem here.

Or is there any other way I can speeden the load (and refresh) time?

Joe Cheng

unread,
Dec 10, 2013, 2:52:11 PM12/10/13
to Rohith V, shiny-...@googlegroups.com
shinyServer(function(input, output, session) {
  values <- reactiveValues(starting = TRUE)
  session$onFlushed(function() {
    values$starting <- FALSE
  })

  output$fast <- renderText({ "This happens right away" })
  output$slow <- renderText({
    if (values$starting)
      return(NULL)
    "This happens later"
  })
})


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Rohith V

unread,
Dec 11, 2013, 12:10:19 AM12/11/13
to shiny-...@googlegroups.com
Thank you here as well Joe :). Didn't know if stack overflow or Google groups was a good place. Hence repeated the post in both places.

Joe Cheng

unread,
Dec 11, 2013, 1:01:33 AM12/11/13
to Rohith V, shiny-...@googlegroups.com
No problem. You can thank me by "accepting" my answer on Stack Overflow. :)


On Tue, Dec 10, 2013 at 9:10 PM, Rohith V <roh...@gmail.com> wrote:
Thank you here as well Joe :). Didn't know if stack overflow or Google groups was a good place. Hence repeated the post in both places.

roh...@gmail.com

unread,
Dec 11, 2013, 1:03:20 AM12/11/13
to Joe Cheng, shiny-...@googlegroups.com
Yes :) I am trying your solution now. Will accept in a few minutes after I retrofit it into my stack.

Rohith V

unread,
Dec 11, 2013, 3:24:55 AM12/11/13
to shiny-...@googlegroups.com
Is this, by any chance applicable to renderText only? I couldn't locate any documentation on session and onFlused events. My renderPlot didn't seem to render quickly.

Rohit

On Wednesday, 11 December 2013 11:33:20 UTC+5:30, Rohith V wrote:
Yes :) I am trying your solution now. Will accept in a few minutes after I retrofit it into my stack.
On Wed, Dec 11, 2013 at 11:31 AM, Joe Cheng <j...@rstudio.com> wrote:
No problem. You can thank me by "accepting" my answer on Stack Overflow. :)
On Tue, Dec 10, 2013 at 9:10 PM, Rohith V <roh...@gmail.com> wrote:
Thank you here as well Joe :). Didn't know if stack overflow or Google groups was a good place. Hence repeated the post in both places.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.

Joe Cheng

unread,
Dec 12, 2013, 3:22:32 AM12/12/13
to Rohith V, shiny-...@googlegroups.com
Hmmm, I swear this worked on my machine before I posted. Try changing

    if (values$starting)
      return(NULL)

to

    if (values$starting) {
      invalidateLater(0, session)
      return(NULL)
    }

Though it would be great if this were not necessary (I'll have to think about it).


To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Rohith V

unread,
Dec 12, 2013, 5:17:39 AM12/12/13
to shiny-...@googlegroups.com, Joe Cheng
Tried this too. No change. I think explaining a little further might help.
  1. I have 4 tabs on the top in my shiny app.
  2. Tab 1 is default active.
  3. Under tab one, I have 15 radio button on the sideBarPanel. 
  4. A click on each one of these radio buttons brings up a barplot and a tabular report in the mainPanel.
  5. Some of the radio buttons even bring up 2 barplot and correlating 2 tabular reports we well.
At this point I hope you can imagine what the layout and scheme looks like?

When I load the app it usually takes 11-13 seconds every time. One interesting observation is that by the time the first barplot is displayed, all other reports are also ready. Meaning after 13 seconds I can click on any of the radio buttons and the the plot is right there.

Hence my thought process was to bring up the first report quickly, in maybe, 3 seconds or so. Then while the analyst is digging thru the first report, the rest of them are processed and made available. With this in mind I posed this question.

When I tried your first suggestion, there was no change in load time. However, I tweaked it to not load the first barplot (renderPlot) of the first radio button as well. But to only bring up the tabular report (renderText) of the first radio button. In this case the tabular report came up in 4 seconds and the the bar plot came up after about 13 seconds. Hence I posed my next question assuming that renderText works but not renderPlot.

I hope this explanation helps.


On Thursday, 12 December 2013 13:52:32 UTC+5:30, Joe Cheng [RStudio] wrote:
Hmmm, I swear this worked on my machine before I posted. Try changing

    if (values$starting)
      return(NULL)

to

    if (values$starting) {
      invalidateLater(0, session)
      return(NULL)
    }

Though it would be great if this were not necessary (I'll have to think about it).
On Wed, Dec 11, 2013 at 12:24 AM, Rohith V <roh...@gmail.com> wrote:
Is this, by any chance applicable to renderText only? I couldn't locate any documentation on session and onFlused events. My renderPlot didn't seem to render quickly.

Rohit

On Wednesday, 11 December 2013 11:33:20 UTC+5:30, Rohith V wrote:
Yes :) I am trying your solution now. Will accept in a few minutes after I retrofit it into my stack.
On Wed, Dec 11, 2013 at 11:31 AM, Joe Cheng <j...@rstudio.com> wrote:
No problem. You can thank me by "accepting" my answer on Stack Overflow. :)
On Tue, Dec 10, 2013 at 9:10 PM, Rohith V <roh...@gmail.com> wrote:
Thank you here as well Joe :). Didn't know if stack overflow or Google groups was a good place. Hence repeated the post in both places.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Stéphane Laurent

unread,
Dec 12, 2013, 5:22:52 AM12/12/13
to shiny-...@googlegroups.com, Joe Cheng
Could you post (a light version of) your code ?

Rohith V

unread,
Dec 12, 2013, 7:40:42 AM12/12/13
to shiny-...@googlegroups.com
Hello Stephane Laurent,


Please let me know if you need more?

Thanks for helping.

Rohit

Stéphane Laurent

unread,
Dec 12, 2013, 7:56:59 AM12/12/13
to shiny-...@googlegroups.com
You have accepted Joe's answer on stackoverflow. Is it another question here ?

Rohith V

unread,
Dec 17, 2013, 1:26:28 AM12/17/13
to shiny-...@googlegroups.com
Oops. Sorry. I tried to do what he suggested and changed my renderText alone. And it worked. As I mentioned earlier, each radio button has a renderPlot and renderText. So the the renderTexts were rendered in 3-4 seconds with Joe's change. But the renderPlot associated with this renderText still took about 13 sec (though just one renderText/renderPlot pair was to rendered instantly). And when this was ready the rest of the report (infact all of the 20 bar charts and their associated tables) were ready as well.

So in essence, this effort lead me to the next question which I posed, "Does this work for renderText only".

Yihui Xie

unread,
Dec 17, 2013, 1:42:26 AM12/17/13
to Rohith V, shiny-discuss
No, I do not see how it is tied to renderText() only. An example:
https://github.com/rstudio/shiny-examples/tree/master/014-onflushed

Regards,
Yihui

Rohith V

unread,
Dec 17, 2013, 1:47:43 AM12/17/13
to shiny-...@googlegroups.com, Rohith V
Oh nice. That code looks helpful. Let me try that and get back.

Thank Yihui.
Reply all
Reply to author
Forward
0 new messages