是否可能让RShiny应用程序的某些部分以延迟的方式执行,就像Windows服务中的延迟启动一样?
让我详细说明。
我有一个带标签的闪亮应用程序。每个选项卡的边栏面板上都有一堆单选按钮。单击每个单选按钮会显示一个报告。我的设置就这么简单。
然而,当我每次加载应用程序时,当第一个选项卡自动呈现时,与该选项卡下的所有单选按钮关联的所有报告都会被执行,然后选择第一个单选按钮并显示其关联报告。整个过程大约需要10-11秒,我想把它记下来。
在服务器启动期间,我只需读取全局中的myData.RData文件。R。所以在服务器启动期间,所有数据都是预先提取的(我假设保存在内存中)。当选项卡聚焦时,会读取myData.RData中的data.frame,并调用一系列ggplots(renderPlot)和表(renderText)。
有没有一种方法可以在几秒钟内呈现第一个报告,然后继续执行其他ggplot和表?我确实经历了反应性导体和隔离,但不知道什么解决方案适合我的问题。
或者有其他方法可以加快加载(和刷新)时间吗?
一些有助于理解问题的代码。。
# 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
}
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"
})
})