![](/img/trans.png)
[英]Download multiple checkboxes in a single csv file via downloadhandler- R Shiny
[英]Download multiple csv files with one button (downloadhandler) with R Shiny
*您好,我正在尝试从一个唯一的 excel 文件下载多个 csv 文件。 我想从 excel 文件下载(仅使用一个下载按钮)差异表。 我不明白为什么 for() 循环不起作用,而且我看不出我该怎么做? 如果有人知道..
重点是下载不同的 csv 文件,这些文件位于“wb”列表中(wb[1]、wb[2]...)谢谢。 例如,这是我使用第三张纸的代码(抱歉我的英语不好):ui:
library(readxl)
library(shiny)
library(XLConnect)
fluidPage(
titlePanel("Export onglets en CSV"),
sidebarLayout(
sidebarPanel(
fileInput('fichier1','Choisissez votre fichier excel :',
accept = ".xlsx"),
fluidPage(
fluidRow(
column(width = 12,
numericInput("sheet","Indiquez l'onglet à afficher :",min = 1, value = 1),
tags$hr(),
textInput('text',"Indiquez le nom des fichiers :"),
tags$hr(),
h4("Pour télécharger les fichiers .csv :"),
downloadButton("download","Télécharger")
)
)
)),
mainPanel(
tabsetPanel(
tabPanel('Importation',
h4("Fichier de base:"),
dataTableOutput("contents"))
)
)
)
)
服务器:
function(input,output){
#Création data :
data <- reactive({
inFile<- input$fichier1
if (is.null(inFile)){
return(NULL)
}else{
file.rename(inFile$datapath,
paste(inFile$datapath,".xlsx", sep =""))
wb = loadWorkbook(paste(inFile$datapath,".xlsx",sep=""))
lst = readWorksheet(wb,sheet = getSheets(wb))
list(wb = wb, lst = lst)
}
})
#Sortie de la table :
output$contents <- renderDataTable({
data()$wb[input$sheet]
},options = list(pageLength = 10))
#Téléchargement :
output$download <- downloadHandler(
#for (i in 1:input$sheet){
filename = function(){
paste(input$text,"_0",3,".csv",sep = "")
},
content = function(file){
write.table(data()$wb[3],file,
sep = ';', row.names = F, col.names = T)
}
#}
)
}
正如@BigDataScientist指出的那样,您可以压缩所有csv文件并下载压缩文件。 您的downloadHandler
可能如下所示:
output$download <- downloadHandler(
filename = function(){
paste0(input$text,".zip")
},
content = function(file){
#go to a temp dir to avoid permission issues
owd <- setwd(tempdir())
on.exit(setwd(owd))
files <- NULL;
#loop through the sheets
for (i in 1:input$sheet){
#write each sheet to a csv file, save the name
fileName <- paste(input$text,"_0",i,".csv",sep = "")
write.table(data()$wb[i],fileName,sep = ';', row.names = F, col.names = T)
files <- c(fileName,files)
}
#create the zip file
zip(file,files)
}
)
这不会从excel文件中下载所有工作表,但工作表范围从1到input$sheet
用户输入的任何内容。
如果用户尚未添加excel文件/名称,您也可以禁用下载按钮。
希望你已经解决了这个MBnn,但是如果其他人遇到类似的问题,这个案例归结为RTools没有在Windows上正确安装。
目前,您需要在运行安装过程时密切关注,并确保点击复选框以编辑系统路径。
根据您的代码,这可能是阻止您保存XLSX工作簿的同一问题。
我知道这是一个旧线程,但我遇到了同样的问题,而且最佳答案对我不起作用。 然而,一个简单的调整和使用存档package 工作。
下面的可重现示例:
library(shiny)
library(archive)
shinyApp(
# ui
ui = fluidPage(downloadButton("dl")),
# server
server = function(input, output, session) {
# download handler
output$dl <- downloadHandler(
filename = function() {"myzipfile.zip"},
# content: iris and mtcars
content = function(file) {
# definition of content to download
to_dl <- list(
# names to use in file names
names = list(a = "iris",
b = "mtcars"),
# data
data = list(a = iris,
b = mtcars)
)
# temp dir for the csv's as we can only create
# an archive from existent files and not data from R
twd <- setwd(tempdir())
on.exit(setwd(twd))
files <- NULL
# loop on data to download and write individual csv's
for (i in c("a", "b")) {
fileName <- paste0(to_dl[["names"]][[i]], ".csv") # csv file name
write.csv(to_dl[["data"]][[i]], fileName) # write csv in temp dir
files <- c(files, fileName) # store written file name
}
# create archive from written files
archive_write_files(file, files)
}
)
}
)
这将创建 zip 文件myzipfile.zip
,其中将包含iris.csv
和mtcars.csv
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.