shingav
shingav

Reputation: 462

Delayed execution in R Shiny app

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?

Some code to help understand the issue..

    # In server.R

    library(shiny)
    library(plyr)
    library(ggplot2)
    library(grid)

    source("a.R", local=TRUE)
    source("b.R", local=TRUE)

    shinyServer(function(input, output) {

      # The below two lines represent a report pair to me. So I have a Bar plot and the associated Table report.
      output$wSummaryPlot = renderPlot({ print(drawBarPlotA("Bar Plot A")) })
      output$wSummaryTable = renderText({ tableA() })

      # There are about 20 such pairs in server.R  

      # Please note that I am including other R file by "source". The first two lines shows that. Don't know if that is what is causing the problem.

      # The drawBarPlotA and tableA are functions defined in one of the source files which are included above. 
      # There are 5 such files which are included similarly.
    })

    # In ui.R
    shinyUI(pageWithSidebar(
        headerPanel(windowTitle = "Perfios - DAS", addHeader()),

        sidebarPanel(
            conditionalPanel(condition = "input.reportTabs == 1 && input.reportType == 'reportTypeA'",
                         wellPanel(radioButtons("showRadio", strong("Attributes:"),
                                                c("Analysis A" = "a", 
                                                  "Analysis B" = "b", 
                                                  "Analysis C" = "c", 
                                                  "Analysis D" = "d",
                                                  "Analysis E" = "e",
                                                  "Analysis F" = "f"
                                                  )))
        ))

        mainPanel(
            tabPanel("A", value = "1", 
                 conditionalPanel(condition = "input.reportType == 'reportTypeA'",
                                  conditionalPanel(condition = "showRadio == 'X'", 
                                                   plotOutput("wSummaryPlot"), h4("Summary:"), verbatimTextOutput("wSummaryTable"))


       # Many such element here to accomodate for those 20 reports... 
    )))
))

# In drawBarPlotA
drawBarPlotA = function(mainText) {
  ggplot(data, aes(variable, value, fill = some_fill)) + 
    geom_bar(stat = "identity", position = "dodge", color = "grey") +
    ylab("Y Label") + 
    xlab(NULL) + 
    theme_bw() + 
    ggtitle(mainText) + 
    scale_fill_brewer(palette = "Set1") +
    annotate("text", x = 1.2, y = 0, label = "Copyright...", size = 4) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1, size = "12", face = "bold"), 
          axis.text.y = element_text(size = "12"),
          plot.title = element_text(size = "14", vjust = 3, face = "bold"))  
}

tableA = function() {
  # This is a data.frame which is returned
  data
}

Upvotes: 9

Views: 7486

Answers (1)

Joe Cheng
Joe Cheng

Reputation: 8061

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"
  })
})

Upvotes: 12

Related Questions