Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed execution in R Shiny app

Tags:

r

shiny

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
}
like image 383
shingav Avatar asked Dec 10 '13 09:12

shingav


People also ask

Is R Shiny difficult?

Along with Shiny elements, you can use HTML elements to stylize your content in your application. In my opinion, R Shiny is very easy to learn despite how powerful the tool is. If you're working on a side project or looking to add something to your portfolio, I highly recommend trying it out.

Is R Shiny useful?

It provides a robust web framework for developing any sort of app, not only dashboards. Shiny is easy and intuitive to use, as you'll see in the examples below. It also doesn't require any HTML, CSS, or JavaScript knowledge – but this is helpful if you want extra customizability.

How does Shiny work in R?

Shiny is based on a reactive programming model, similar to a spreadsheet. Spreadsheet cells can contain literal values, or formulas that are evaluated based on other cells. Whenever the value of the other cells change, the value of the formula is automatically updated. Shiny apps behave the same way.


1 Answers

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"
  })
})
like image 109
Joe Cheng Avatar answered Oct 18 '22 21:10

Joe Cheng