我对 Shiny 很陌生,如果我的问题很简单,我深表歉意。我尝试查看 google 和 stackoverflow,但到目前为止找不到简单而有用的答案。 我的目标/问题是什么:我正在编写一个 Shiny 的页面,该页面显示一个包含数十万行的表格。 数据来自不同的数据库,经过处理、清理并根据请求显示给所有用户。 问题一:为了加载所有的数据,脚本差不多用了5分钟 问题 2:如果 user1 在上午 8:00 请求此数据,而在 user2 上午 8:05 请求相同的数据,则会启动两个不同的查询,并且还会使用内存中的两个不同空间向两个不同的用户显示完全相同的数据。 所以问题是:我应该使用缓存系统来增强这个过程吗? 如果没有,我还能用什么? 我发现了很多关于缓存图的官方 Shiny 文档,但与缓存数据无关(我发现这很令人惊讶)。 其他有用信息:缓存中的数据应在每天晚上 10 点左右删除,因为新数据将在第二天/清晨可用。
代码:
ui <- dashboardPage( # https://rstudio.github.io/shinydashboard/structure.html
title = "Dashboard",
dashboardHeader(title = "Angelo's Board"),
dashboardSidebar( # inside here everything that is displayed on the left hand side
includeCSS("www/styles.css"),
sidebarMenu(
menuItem('menu 1', tabName = "menu1", icon = icon("th"),
menuItem('Data 1', tabName = 'tab_data1'))
)),
dashboardBody(
tabItems(
tabItem(tabName = 'tab_data1')),
h3("Page with big table"),
fluidRow(dataTableOutput("main_table"))
))
server <- function(input, output, session) {
output$main_tabl <- renderDataTable({
df <- data.frame(names = c("Mark","George","Mary"), age = c(30,40,35))
})
}
cat("\nLaunching 'shinyApp' ....")
shinyApp(ui, server)
我用来检查潜在解决方案的资源:
- How to cache data in shiny server?但显然我不能使用 Jason Bryer 包
- https://shiny.rstudio.com/reference/shiny/1.2.0/memoryCache.html但我不知道如何将此代码应用于我的示例
- https://shiny.rstudio.com/articles/plot-caching.html主要关注剧情缓存
任何帮助将不胜感激。谢谢
最佳答案
我会将您的大部分 ETL 过程分解为单独的 R 脚本,并将该脚本设置为在 cron 上运行。然后,您可以让此脚本将处理后的数据帧写出到 .feather 文件。然后让你 Shiny 的应用程序加载羽毛文件 - 羽毛针对阅读进行了优化,因此应该很快。
例如,从您的 server.R(或 app.R)文件中取出必要的库和代码,并创建一个名为 query.R 的新 R 脚本。该脚本执行所有 ETL 操作并最终将您的数据写出到 .feather 文件(需要 feather 包)。然后创建一个 crontab 以根据需要经常运行该脚本。
然后您的 server.R 脚本只需要在应用加载时读入该 feather 文件,您应该会看到显着的性能提升。此外,您还让 query.R 脚本在下类时间运行,这样 linux 机器上的性能就不会受到负面影响。
关于R Shiny - 缓存大数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59584808/