Search code examples
rshinyzip

Is there a way to upload multiple .zip files within an [R] Shiny application and select a specific file type to extract?


I am currently only able to deal with one .zip archive at a time even if I set "multiple=T":

ui.R {
...fileInput("zippers", "upload .zip files",multiple=T,accept = ".zip"),
}

I can retrieve the names of the files I'm interested in (found in another topic) for a single instance as well:

server.R{
zipped_csv_names <- grep('\\.csv$', unzip(input$zippers$datapath, list=TRUE)$Name, ignore.case=TRUE, value=TRUE) 
}

if I try two or more files I get the "Error: invalid zip name argument"


Solution

  • This returns a list of data.frames with the path to the zip file and the names of the csv files inside.

    server <- function(input, output) {
    
      v <- reactiveValues(zipped_csv_names = NULL)
    
      observeEvent(input$zippers, {
        csv_names <- lapply(input$zippers$datapath, function(x){
          z <- unzip(x, list = T)
          csvFile <- grep("*.csv", z$Name, value = T)
          if (length(csvFile) > 0)
            return(data.frame(fPath = rep(x, n = length(csvFile)), fName = csvFile))
        })
        v$zipped_csv_names <- csv_names[!sapply(csv_names, is.null)]
      })
    }