如何将现有文件夹下载为 zip?
How to download an existing folder as zip?
我想知道如何下载文件夹及其中的所有文件。我试过这个:
UI.
downloadButton("downloadresults")
服务器
#To create the directory where store the files
dir.create(session$token)
#Action on download button
output$download <- downloadHandler(
filename <- function(){
paste("Results","zip", sep = ".")
},
content <- function(file) {
path <- session$token
files <- list.files(path, recursive = T)
zip(file, files)
},
contentType = " application/zip"
)
此 returns 一个 html 文件。为什么会这样?如何主动将文件夹下载为 .zip。
编辑一个
由于@YBS 的评论,我更改了 UI:
downloadButton("download")
这不会下载文件,我也没有收到任何错误或警告消息
编辑两个
到目前为止,我已经成功地使用以下代码完成了文件的下载:
UI:
downloadButton("download ")
服务器
#To create the directory where store the files
dir.create(session$token)
#Action on download button
output$download <- downloadHandler(
filename = function(){
paste(" Results", "zip", sep = ".")
},
content = function(file) {
path <- session$token
zip::zipr(file, path)
},
contentType = " application/zip"
)
我明白了:
Results.zip
- session$token 文件夹
- 文件名.hdf5
- filename.csv
我想要这个:
Results.zip
- 文件名.hf5
- filename.csv
根据@YBS给出的答案,我设法解决了如下问题:
library(shiny)
library(zip)
ui <- fluidPage(
#Select a number
numericInput("firstnumber", "Select a number", 1, min = 1),
#Select another number
numericInput("secondnumber", "Select a number", 1, min = 1),
#Make calculation
actionButton("calculation","GO"),
#Alert results are ready
uiOutput("alert"),
#Download button
downloadButton("download")
)
server <- function(input, output, session) {
#To create directory
dir.create(session$token)
#To create folder inside directory
FOLDER <- paste0(session$token,"/Folder")
dir.create(FOLDER)
#Reactive value to store the path of all the files in the directory
mylist <- reactiveValues()
mylist$files <- NULL
#Go button action
observeEvent(input$calculation,{
#Calculations
SUM <- input$firstnumber + input$secondnumber
SUBSTRACTION <- input$firstnumber - input$secondnumber
DIVIDE <- input$firstnumber/input$secondnumber
MULTIPLY <- input$firstnumber * input$secondnumber
#Storing results in a data frame
S_and_S <- data.frame(SUM,SUBSTRACTION)
D_and_M <- data.frame(DIVIDE,MULTIPLY)
#Alert results are ready
output$alert <- renderUI({
h5("Results are ready", style = "color:green")
})
#Saving data frame in directory as Results.csv
write.csv(S_and_S, file = paste0(session$token,"/Results.csv"))
#Savin data frame in directory as results.csv
write.csv(D_and_M, file = paste0(FOLDER,"/results.csv"))
#Store all the path of the files in the directory in the reactive value
mylist$files <- list.files(session$token,"*.*")
})
#Action on download button
output$download <- downloadHandler(
filename = function(){
"Results.zip"
},
content = function(file){
files <- c()
for (i in 1:length(mylist$files)) {
files[i] <- paste0(session$token,"/",mylist$files[i])
}
zipr(zipfile = file, files = files)
},
contentType = "application/zip"
)
}
shinyApp(ui, server)
我想知道如何下载文件夹及其中的所有文件。我试过这个:
UI.
downloadButton("downloadresults")
服务器
#To create the directory where store the files
dir.create(session$token)
#Action on download button
output$download <- downloadHandler(
filename <- function(){
paste("Results","zip", sep = ".")
},
content <- function(file) {
path <- session$token
files <- list.files(path, recursive = T)
zip(file, files)
},
contentType = " application/zip"
)
此 returns 一个 html 文件。为什么会这样?如何主动将文件夹下载为 .zip。
编辑一个
由于@YBS 的评论,我更改了 UI:
downloadButton("download")
这不会下载文件,我也没有收到任何错误或警告消息
编辑两个
到目前为止,我已经成功地使用以下代码完成了文件的下载:
UI:
downloadButton("download ")
服务器
#To create the directory where store the files
dir.create(session$token)
#Action on download button
output$download <- downloadHandler(
filename = function(){
paste(" Results", "zip", sep = ".")
},
content = function(file) {
path <- session$token
zip::zipr(file, path)
},
contentType = " application/zip"
)
我明白了:
Results.zip
- session$token 文件夹
- 文件名.hdf5
- filename.csv
我想要这个:
Results.zip
- 文件名.hf5
- filename.csv
根据@YBS给出的答案,我设法解决了如下问题:
library(shiny)
library(zip)
ui <- fluidPage(
#Select a number
numericInput("firstnumber", "Select a number", 1, min = 1),
#Select another number
numericInput("secondnumber", "Select a number", 1, min = 1),
#Make calculation
actionButton("calculation","GO"),
#Alert results are ready
uiOutput("alert"),
#Download button
downloadButton("download")
)
server <- function(input, output, session) {
#To create directory
dir.create(session$token)
#To create folder inside directory
FOLDER <- paste0(session$token,"/Folder")
dir.create(FOLDER)
#Reactive value to store the path of all the files in the directory
mylist <- reactiveValues()
mylist$files <- NULL
#Go button action
observeEvent(input$calculation,{
#Calculations
SUM <- input$firstnumber + input$secondnumber
SUBSTRACTION <- input$firstnumber - input$secondnumber
DIVIDE <- input$firstnumber/input$secondnumber
MULTIPLY <- input$firstnumber * input$secondnumber
#Storing results in a data frame
S_and_S <- data.frame(SUM,SUBSTRACTION)
D_and_M <- data.frame(DIVIDE,MULTIPLY)
#Alert results are ready
output$alert <- renderUI({
h5("Results are ready", style = "color:green")
})
#Saving data frame in directory as Results.csv
write.csv(S_and_S, file = paste0(session$token,"/Results.csv"))
#Savin data frame in directory as results.csv
write.csv(D_and_M, file = paste0(FOLDER,"/results.csv"))
#Store all the path of the files in the directory in the reactive value
mylist$files <- list.files(session$token,"*.*")
})
#Action on download button
output$download <- downloadHandler(
filename = function(){
"Results.zip"
},
content = function(file){
files <- c()
for (i in 1:length(mylist$files)) {
files[i] <- paste0(session$token,"/",mylist$files[i])
}
zipr(zipfile = file, files = files)
},
contentType = "application/zip"
)
}
shinyApp(ui, server)