【发布时间】:2020-07-15 18:24:30
【问题描述】:
上下文:我有一个应用程序根据用户的选择转换数据。它在此过程中创建了一些表格和绘图。
目标:一键将过程中创建的一些对象保存到一个新文件夹中。
以前的研究: 下面的代码使用downloadHandler() 和一些函数来保存对象here。它似乎不允许将多个对象传递给downloadHandler()。我知道可以将这些对象堆叠在一个列表中然后保存它,但如果可能的话,我想避免这样做,而是获取多个文件(如 .txt 或 .png,...)
这是一个使用 R 中包含的数据集(mtcars 和 iris)的数据非常少的可重现示例。
library(shiny)
ui <- fluidPage(
downloadButton("save", "Save") # one click on this button to save df1 AND df2 tables in a new folder
)
server <- function(input, output) {
# my real app does multiple changes on datasets based on user choices
df1 = mtcars[1:10,]
df2 = iris[1:10,]
# Now I want to save df1 and df2 objects with 1 click on the "Save" button
output$save = downloadHandler(
filename = function(){ paste("example", ".txt", sep = " ") },
content = function(file) { write.table(df1, file) }
)
}
# Run the application
shinyApp(ui = ui, server = server)
非常感谢您的帮助和建议!
【问题讨论】:
-
我认为您需要压缩文件,然后将 zip 传递给 downloadHandler