R/Shiny:从服务器上的文件夹下载多个文件 (zip) [英] R/Shiny: Download multiple files (zip) from a folder on the server

查看:134
本文介绍了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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆