如何更新 .RDS文件与用户上传的文件并存储它以备下次在Shiny(服务器)中使用?



基本上,有 50 个文件,每个文件大约 50 MB,因此,处理它们并使 30k 行处理数据为 .RDS 文件来呈现一些可视化效果。 但是,用户需要应用程序上传最近的文件并不断更新可视化效果。

是否可以更新 .包含用户上传的文件的 RDS 文件?用户可以访问此更新的 .RDS 文件甚至下次(会话(?

在以下示例中, 有一个上传按钮,只渲染一个文件。 我们可以以某种方式存储上传的文件吗? 因此,我们可以使用这些上传的文件来更新.RDS 文件

相关链接: R 闪亮保存到服务器 https://shiny.rstudio.com/articles/persistent-data-storage.html

library(shiny)
# Define UI for data upload app ----
ui <- fluidPage(
# App title ----
titlePanel("Uploading Files"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("files", "Upload", multiple = TRUE, accept = c(".csv"))
),
# In order to update the database if the user clicks on the action button
actionButton("update_database","update Database")
# Main panel for displaying outputs ----
mainPanel(
# Output: Data file ----
dataTableOutput("tbl_out")
)
)
)
# Define server logic to read selected file ----
server <- function(input, output) {
lst1 <- reactive({
validate(need(input$files != "", "select files..."))
##### Here, .RDS file can be accessed and updated for visualiztaion
if (is.null(input$files)) {
return(NULL)
} else {
path_list <- as.list(input$files$datapath)
tbl_list <- lapply(input$files$datapath, read.table, header=TRUE, sep=";")
df <- do.call(rbind, tbl_list)
return(df)
}
})
output$tbl_out <- renderDataTable({
##### Can also access this data for visualizations
lst1()
})
session$onSessionEnded({
observeEvent(input$update_database,{
s3save(appended_data, object = "object_path_of_currentRDS_file",bucket = "s3_bucket")
})
})
}
# Create Shiny app ----
shinyApp(ui, server)
But there was an error:
Warning: Error in private$closedCallbacks$register: callback must be a function
50: stop
49: private$closedCallbacks$register
48: session$onSessionEnded
47: server [/my_folder_file_path.R#157]
Error in private$closedCallbacks$register(sessionEndedCallback) : 
callback must be a function

解决方案:

替换此代码

session$onSessionEnded({
observeEvent(input$update_database,{
s3save(appended_data, object = "object_path_of_currentRDS_file",bucket = "s3_bucket")
})
})
## 与此
observeEvent(input$update_database,{
s3saveRDS(appended_data, object = "object_path_of_currentRDS_file",bucket = "s3_bucket")
})

最新更新