我当前在闪亮的应用程序中的工作流程是定期运行R脚本作为cron作业,以从多个数据库中提取各种表以及从某些API下载数据。然后将它们另存为.Rdata文件在名为的文件夹中data
。
在我的global.R
文件中,我使用加载数据load("data/workingdata.Rdata")
。这导致所有数据帧(约30个)加载到环境中。我知道我可以使用该reactiveFileReader()
功能刷新数据,但是显然由于与该功能相关联的会话,因此必须在server.R文件中使用它。另外,我不确定是否load
接受readFunc
in reactiveFileReader()
。在这里,该方案的最佳策略应该是什么?
本示例使用reactiveVal
带有observe
和的对象invalidateLater
。数据将被加载到新环境中,并每2秒分配给reactVal。
library(shiny)
ui <- fluidPage(
actionButton("generate", "Click to generate an Rdata file"),
tableOutput("table")
)
server <- shinyServer(function(input, output, session) {
## Use reactiveVal with observe/invalidateLater to load Rdata
data <- reactiveVal(value = NULL)
observe({
invalidateLater(2000, session)
n <- new.env()
print("load data")
env <- load("workingdata.Rdata", envir = n)
data(n[[names(n)]])
})
## Click the button to generate a new random data frame and write to file
observeEvent(input$generate, {
sample_dataframe <- iris[sample(1:nrow(iris), 10, F),]
save(sample_dataframe, file="workingdata.Rdata")
rm(sample_dataframe)
})
## Table output
output$table <- renderTable({
req(data())
data()
})
})
shinyApp(ui = ui, server = server)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句