使用Shining downloadHandler将多个数据帧保存到单个csv文件
我有一个RShiny应用程序,我在不同的选项卡设置面板上显示多个数据帧。我希望能够将显示在每个面板上的数据帧写入每个面板的一个csv文件。我目前正在尝试使用Shinny的downloadHandler选项来渲染它 做了一点研究,发现我可以使用sink()函数将R输出转移到一个文件 下面是我试图将sink()合并到server.R端的一个这样的按钮中的尝试使用Shining downloadHandler将多个数据帧保存到单个csv文件,r,export-to-csv,shiny,R,Export To Csv,Shiny,我有一个RShiny应用程序,我在不同的选项卡设置面板上显示多个数据帧。我希望能够将显示在每个面板上的数据帧写入每个面板的一个csv文件。我目前正在尝试使用Shinny的downloadHandler选项来渲染它 做了一点研究,发现我可以使用sink()函数将R输出转移到一个文件 下面是我试图将sink()合并到server.R端的一个这样的按钮中的尝试 output$downloadDemographic <- downloadHandler( filename = function()
output$downloadDemographic <- downloadHandler(
filename = function() {
paste('Demographics', '.csv', sep='')
},
content = function(file) {
sink("Demographics.csv")
cat('Population Demographics')
write.csv(Population)
cat('Geography Demographics')
write.csv(Geography)
sink()
}
)
输出$downloaddographic其他选项:
下面是使用R数据集包中的四个数据帧的两个选项的示例
library(shiny)
library(xlsx)
shinyApp(
ui = fluidPage(
downloadButton("downloadExcelSheet", "Download Excel Workbook with Multiple Sheets"),
downloadButton("downloadZippedCSV", "Download zipped csv files")
),
server = function(input, output) {
#### Write an Excel workbook with one sheet per dataframe ####
output$downloadExcelSheet <- downloadHandler(
filename = function() {
"excelWorkbook.xlsx"
},
content = function(file) {
# write workbook and first sheet
write.xlsx(mtcars, file, sheetName = "mtcars", append = FALSE)
# add other sheets for each dataframe
listOtherFiles <- list(iris = iris,
airquality = airquality,
sleep = sleep)
for(i in 1:length(listOtherFiles)) {
write.xlsx(listOtherFiles[i], file,
sheetName = names(listOtherFiles)[i], append = TRUE)
}
}
)
#### Zip together multiple csv files ####
output$downloadZippedCSV <- downloadHandler(
filename = function() {
"zippedCSV.zip"
},
content = function(file) {
# go to temp dir to avoid permission issues
owd <- setwd(tempdir())
on.exit(setwd(owd))
# create list of dataframes and NULL value to store fileNames
listDataFrames <- list(mtcars = mtcars,
iris = iris,
airquality = airquality,
sleep = sleep)
allFileNames <- NULL
# loop through each dataframe
for(i in 1:length(listDataFrames)) {
# write each dataframe as csv and save fileName
fileName <- paste0(names(listDataFrames)[i], ".csv")
write.csv(listDataFrames[1], fileName)
allFileNames <- c(fileName, allFileNames)
}
# write the zip file
zip(file, allFileNames)
}
)
}
)
库(闪亮)
图书馆(xlsx)
shinyApp(
ui=fluidPage(
下载按钮(“下载Excel工作表”,“下载包含多个工作表的Excel工作簿”),
下载按钮(“下载压缩的csv”、“下载压缩的csv文件”)
),
服务器=函数(输入、输出){
####编写Excel工作簿,每个数据框一张工作表####
output$downloadExcelSheet谢谢@BlondClover,你真是个救命稻草!我喜欢第一个可以将工作簿与不同工作表缝合在一起的示例。Excel上的格式设置有点不正确,但将文本转换为数字对我来说很有帮助。我仍然希望能够将这两个数据帧解析到同一工作表上。每个选项卡集面板在我的闪亮应用程序中,应该显示不同的信息片段——在本例中,“人口”和“地理”是两个不同的数据帧,用于传递有关“人口统计”的信息我的账单Application@AshwinRamesh您的用户是否需要能够编辑下载的表或将其导入R?如果不需要,您可以使用RMarkdown制作一个PDF,使两个表位于同一位置page@blonderclover表格需要作为Excel文件输出。应用程序根据用户提供的输入计算14个数据帧(Csv文件上传)。这些数据框出现在7个选项卡面板上。将它们下载到14个excel表格将涉及一些后处理工作,因为其中一些数据框本质上是属于该特征的不同度量。如属于人口统计特征的人口和地理。7个excel表格-每个选项卡一个表格将被执行你的解决方案和我最初的问题结合起来。