Readr read all csv files in zip archive

Hi,
Thanks for the suggestions. I guess I did this the hard way:

proc_csv <- function(inZip,varList){  #inZip is the ZIP Archive, varList is the variable specifications from readr
  outFile <- data.frame()
  
  filList <- unzip(inZip, list = TRUE) # Create list of files
  for(j in 1:nrow(filList)) { # Loop through the list of files
    if(grepl("csv",filList[j,1])) {  #If a file is a csv file, unzip it and read the data
      oFa <- read_csv(unz(inZip, filList[j,1]),col_names=TRUE, col_types = varList)
      outFile <- rbind(outFile,oFa)    #Then add the data files together
    }
  }
  outFile <- outFile[,c("PUMA","ST","MIGPUMA","MIGSP","PWGTP")]  #Finally, select the required variables
        #In my case, the variables in the archive vary by year, so this step is necessary
  return(outFile)  
}

This is not especially elegant, but it is a general solution. It would be great if there was an option in read_csv that allowed reading all of the csv files found in a ZIP archive.
Cheers

1 Like