R/Shiny: Download multiple files (zip) from a fold

2019-08-30 02:00发布

问题:

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 dinamycally 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=" ")))


标签: r unix shiny zip sh