gpt4 book ai didi

r - R Shiny应用程序中的执行延迟

转载 作者:行者123 更新时间:2023-12-04 03:38:12 24 4
gpt4 key购买 nike

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

让我详细说明。

我有一个带有标签的 Shiny 应用程序。每个选项卡在sidebarPanel上都有一堆单选按钮。单击每个单选按钮将显示一个报告。我的设置就这么简单。

但是,当我每次加载应用程序时以及自动渲染第一个选项卡时,都会执行与该选项卡下所有单选按钮关联的所有报告,然后选择第一个单选按钮并显示其相关报告。这整个过程大约需要10-11秒,我希望将其降低。

在服务器启动期间,我只需要读取global.R中的myData.RData文件即可。因此,在服务器启动期间,所有数据都已预取(我假设已保存在内存中)。当标签成为焦点时,将发生以下情况:读取myData.RData中的data.frames,并调用一系列ggplots(renderPlot)和表(renderText)。

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

还是有其他方法可以加快加载(和刷新)时间?

一些代码可以帮助您理解问题。

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

关于r - R Shiny应用程序中的执行延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20490619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com