R/Shiny: Download multiple files (zip) from a folder on the server

丶灬走出姿态 提交于 2019-12-11 17:17:28

问题


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.

Here the code for the checkbox:

  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])  )
    })
})

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

The function for the download is:

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.

The error that I get when I try to download the zip file is:

sh: : command not found

Can someone help me to fix this?


回答1:


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).

Instead of

zip(file,files) 

I have to use:

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


来源:https://stackoverflow.com/questions/54183183/r-shiny-download-multiple-files-zip-from-a-folder-on-the-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!