R 下载包含数据表的多个被动对象
在我闪亮的应用程序R 下载包含数据表的多个被动对象,r,shiny,R,Shiny,在我闪亮的应用程序(RA_s,Per)中,我在反应对象中有多个表,我分别下载每个表。现在,我尝试使用下载按钮下载一个包含所有这些表的zip文件 这是我的代码,我不知道如何完成downloadHandler功能。我想下载一个zip文件,其中包含两个csv文件RA_s和Per 代码 shinyServer(function(input, output) { RA_s <- reactive({ iris }) Per <- reactive({ sepal
(RA_s,Per)
中,我在反应对象中有多个表,我分别下载每个表。现在,我尝试使用下载按钮下载一个包含所有这些表的zip文件
这是我的代码,我不知道如何完成downloadHandler
功能。我想下载一个zip文件,其中包含两个csv文件RA_s
和Per
代码
shinyServer(function(input, output) {
RA_s <- reactive({
iris
})
Per <- reactive({
sepal1 <- RA_s()["Sepal_Length"]
sepal2 <- RA_s()["Sepal_Width"]
value = sepal1*sepal2
c = cbind(RA_s(),value)
})
output$downloadData <- downloadHandler(
filename = function() {
paste0("output", ".zip")
},
content = function(file) {
...
})
})
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Download", tabName = "d")
)
body<- dashboardBody(
tabItems(
tabItem(tabName = "d",
downloadButton('downloadData', 'Download')
)
)
dashboardPage(
dashboardHeader(title = "Valo"),
sidebar,
body
)
shinyServer(功能(输入、输出){
RA_s您可以先保存压缩文件,然后压缩它们,如下所示:
library(shiny)
server<- shinyServer(function(input, output) {
RA_s <- reactive({
iris
print(iris)
})
Per <- reactive({
sepal1 <- RA_s()["Sepal.Length"]
sepal2 <- RA_s()["Sepal.Width"]
value = sepal1*sepal2
c = cbind(RA_s(),value)
})
output$downloadData <- downloadHandler(
filename = 'two_csvs.zip',
content = function(fname) {
write.csv(RA_s(), file = "csv1.csv", sep =",")
write.csv(Per(), file = "csv2.csv", sep =",")
zip(zipfile=fname, files=c("csv1.csv","csv2.csv"))
},
contentType = "application/zip"
)
})
ui<- shinyUI(
downloadButton('downloadData', 'Download')
)
shinyApp(ui,server)
库(闪亮)
服务器您可以先保存zip文件,然后对其进行压缩,如下所示:
library(shiny)
server<- shinyServer(function(input, output) {
RA_s <- reactive({
iris
print(iris)
})
Per <- reactive({
sepal1 <- RA_s()["Sepal.Length"]
sepal2 <- RA_s()["Sepal.Width"]
value = sepal1*sepal2
c = cbind(RA_s(),value)
})
output$downloadData <- downloadHandler(
filename = 'two_csvs.zip',
content = function(fname) {
write.csv(RA_s(), file = "csv1.csv", sep =",")
write.csv(Per(), file = "csv2.csv", sep =",")
zip(zipfile=fname, files=c("csv1.csv","csv2.csv"))
},
contentType = "application/zip"
)
})
ui<- shinyUI(
downloadButton('downloadData', 'Download')
)
shinyApp(ui,server)
库(闪亮)
ServerThank@Florian它工作得很好。我如何指定一个目录来保存我的zip文件?您可以通过执行write.csv(RA_s(),file=“dirname/csv1.csv”,sep=“,”)来指定csv的目录,其中DrNEX应该是您的目录的名称。请注意目录必须工作,您可以在同一个文件夹中创建一个文件夹“存储”,例如,您的UI.R和Server .R。如果有帮助的话,请考虑接受我的回答,谢谢!我还有一个小问题。除了ZIP文件,CSV文件。也可以在我的工作目录中单独下载。我认为它们是由于write.csv语句而创建的。您是否在工作目录中创建了其他文件夹,并根据我以前的评论调整了write csv语句?是的,但不起作用。我有以下错误:“错误:无法打开连接”谢谢@Florian it works很好。如何指定要保存zip文件的目录?您可以通过执行write.csv(RA_s(),file=“dirname/csv1.csv”,sep=“,”)来指定csv的目录,其中DrNEX应该是您的目录的名称。请注意目录必须工作,您可以在同一个文件夹中创建一个文件夹“存储”,例如,您的UI.R和Server .R。如果有帮助的话,请考虑接受我的回答,谢谢!我还有一个小问题。除了ZIP文件,CSV文件。也可以在我的工作目录中单独下载。我认为它们是由于write.csv语句而创建的。您是否在工作目录中创建了其他文件夹,并根据我以前的评论调整了write csv语句?是的,但不起作用。我有以下错误:“错误:无法打开连接”