2013-12-10 60 views
8

RShiny應用程序的某些部分是否可能以延遲方式執行,就像Windows服務中的延遲啓動一樣?在R Shiny應用程序中延遲執行

讓我詳細說明。

我有一個閃亮的應用程序與選項卡。每個選項卡在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 
} 
+0

你能告訴我們你的'server.R','ui.R'和任何其他必要的代碼? – MadScone

+0

我已經添加了一段代碼。希望這可以幫助。謝謝。 – shingav

回答

9
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" 
    }) 
}) 
+0

非常感謝Joe。 – shingav

+0

這是否適用於renderText?我找不到有關會話和onFlused事件的任何文檔。我的渲染圖似乎沒有改善。 – shingav

+0

@RohithVenkatakrishna不,我不認爲它綁定到'renderText()';你可以使用任何'render ???'函數。 –