RShiny 应用程序的某些部分是否可能以延迟方式执行,就像 Windows 服务中的延迟启动一样?
让我详细说明一下。
我有一个带有选项卡的闪亮应用程序。每个选项卡的侧边栏面板上都有一堆单选按钮。单击每个单选按钮都会弹出一个报告。我的设置就是这么简单。
但是,当我每次加载应用程序并且自动渲染第一个选项卡时,都会执行与该选项卡下的所有单选按钮关联的所有报告,然后选择第一个单选按钮并显示其相关报告。整个过程大约需要 10-11 秒,我想把它记下来。
在服务器启动期间,我只需读取 global.R 中的 myData.RData 文件。因此,所有数据都是在服务器启动期间预取的(我假设保存在内存中)。当选项卡获得焦点时,会读取 myData.RData 中的 data.frames,并调用一系列 ggplot (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
}