runzipfread

preventing easycsv::fread_zip from saving extracted files


I want to load (many) .zip files, each containing (many) txt files. I understand that one simple way is to use easycsv::fread_zip (or easycsv::fread_folder for multiple zip files). This seems to work but has the undesirable property of automatically saving all the extracted files in my R project root directory, which seems to be an undocumented feature of those functions (unless I missed it!). Is there a way to prevent this? Or is there an alternative to easycsv::fread_zip? thanks

So for example

require(easycsv)
  filezip <- system.file("exampleZips", "example_tables.zip", package="easycsv")
  fread_zip(filezip)

  fread_zip(filezip, extension = "CSV")

creates these files in my R project root directory

example


Solution

  • Change to a temporary directory, run fread_zip and change back. The temporary directory will be removed when you exit R.

    Note that require should only be used within an if to ensure that if there is a problem it is caught at that point. To load a package without if use library.

    library(easycsv)
    
    odir <- setwd(tempdir(check = TRUE))
    fread_zip(filezip)
    setwd(odir)
    

    Alternately consider using your own function. This does not use easycsv except for the input test file.

    library(data.table)
    
    # fread those files from zip file at full path x whose names match pat
    # returning list of data tables.
    read_zip <- function(x, pat = "\\.csv$") {
      on.exit({ file.remove(Files); setwd(odir) })
      odir <- setwd(tempdir(check = TRUE))
      Files <- unzip(zipfile = x)
      Map(fread, grep(pat, Files, value = TRUE))
    }
    
    # test
    filezip <- system.file("exampleZips", "example_tables.zip", package="easycsv")
    L <- read_zip(filezip)
    # list2env(L, .GlobalEnv) # optionally put data tables in global env