在R Shiny app中延迟执行

时间:2013-12-10 09:30:36

标签: r shiny

是否有可能让RShiny应用程序的某些部分以延迟的方式执行,就像Windows服务中的延迟启动一样?

让我详细说明。

我有一个带标签的闪亮应用。每个选项卡在sidebarPanel上都有一堆单选按钮。单击每个单选按钮会显示一个报告。我的设置就像这样简单。

但是,当我每次加载应用程序时以及第一个选项卡自动呈现时,将执行与此选项卡下所有单选按钮关联的所有报告,然后选择第一个单选按钮并显示其关联报告。整个过程大约需要10-11秒,我想放下。

在服务器启动期间,我只是在global.R中读取myData.RData文件。因此,在服务器启动期间,所有数据都是预取的(我假设保留在内存中)。使选项卡聚焦时会发生什么,读取myData.RData中的data.frames并调用一系列ggplots(renderPlot)和表格(renderText)。

有没有办法可以在几秒钟内呈现第一个报告,然后继续执行其他ggplots和表格?我确实通过反应导体和隔离,但无法确定哪种解决方案符合我的问题。

或者还有其他方法可以让您加载(和刷新)时间吗?

一些有助于理解问题的代码..

    # 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
}

1 个答案:

答案 0 :(得分:12)

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