R/Shiny:从服务器上的文件夹下载多个文件 (zip)

编程入门 行业动态 更新时间:2024-10-25 16:25:48
本文介绍了R/Shiny:从服务器上的文件夹下载多个文件 (zip)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个 zip 存档(包含多个 xlsx 文件)并将其保存在本地.这些文件存储在服务器端的文件夹中.用户使用 checkboxInput 选择要压缩的文件.

I would like to create a zip archive (containing several xlsx files) and save it locally. The files are stored in a folder on the server side. The user selects the files to zip using a checkboxInput.

这里是复选框的代码:

get.files <- reactive({ list.files("output_file/") }) obsList <- list() output$links_list <- renderUI({ lapply(as.list(1:length(get.files())), function(i) { btName <- get.files()[i] # creates an observer only if it doesn't already exists if (is.null(obsList[[btName]])) { obsList[[btName]] <<- btName } fluidRow(checkboxInput(btName, get.files()[i]) ) }) })

复选框是动态创建的,读取文件夹中的内容(output_file/").每个复选框旁边都有文件名.

The checkboxes are created dynamically reading the content in the folder ("output_file/"). Near each checkbox there is the name of the file.

下载的功能是:

output$downloadzip<-downloadHandler( filename = function(){ paste0("Extract.zip") }, content = function(file){ files <- NULL; for (i in 1:length(obsList)){ if(input[[obsList[[i]]]]) files <- c(paste("output_file/",obsList[[i]],sep=""),files) } #create the zip file zip(file,files) }, contentType = "application/zip" )

该函数仅使用已检查文件的名称创建文件名(文件)数组.

The function creates an array of filenames (files) using only the names of files that have been checked.

我还创建了一个函数,允许我检查是否只选择了正确的文件:

I have created also a function that allows me to check that only the right files are chosen:

tempText <- eventReactive({input$TempTest},{ l<-c() for (i in 1:length(obsList)){ if(input[[obsList[[i]]]]) l<-c(l,paste("output_file/",obsList[[i]],sep="")) } return(paste(l) ) }, ignoreInit = TRUE) output$Temp <- renderPrint({ tempText()})

此函数正确呈现带有文件名的字符串.

This function renders correctly the strings with the name of the files.

我在尝试下载 zip 文件时遇到的错误是:

sh:: 未找到命令

有人可以帮我解决这个问题吗?

Can someone help me to fix this?

推荐答案

我已经解决了这个问题.问题在于 zip 功能由于某些原因在我的服务器上无法正常工作.解决办法是直接使用system2函数(即zip内部调用).

I have fixed the problem. The issue is with the zip function that for some reasons doesn't work properly on my server. The solution is to use directly the system2 function (that is called internally by zip).

代替

zip(file,files)

我必须使用:

system2("zip", args=(paste(file,files,sep=" ")))

更多推荐

R/Shiny:从服务器上的文件夹下载多个文件 (zip)

本文发布于:2023-05-28 06:52:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/315061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文件夹   器上   文件   Shiny

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!