Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate zip file reading in R

Tags:

I need to automate R to read a csv datafile that's into a zip file.

For example, I would type:

read.zip(file = "myfile.zip") 

And internally, what would be done is:

  • Unzip myfile.zip to a temporary folder
  • Read the only file contained on it using read.csv

If there is more than one file into the zip file, an error is thrown.

My problem is to get the name of the file contained into the zip file, in orded to provide it do the read.csv command. Does anyone know how to do it?

UPDATE

Here's the function I wrote based on @Paul answer:

read.zip <- function(zipfile, row.names=NULL, dec=".") {     # Create a name for the dir where we'll unzip     zipdir <- tempfile()     # Create the dir using that name     dir.create(zipdir)     # Unzip the file into the dir     unzip(zipfile, exdir=zipdir)     # Get the files into the dir     files <- list.files(zipdir)     # Throw an error if there's more than one     if(length(files)>1) stop("More than one data file inside zip")     # Get the full name of the file     file <- paste(zipdir, files[1], sep="/")     # Read the file     read.csv(file, row.names, dec) } 

Since I'll be working with more files inside the tempdir(), I created a new dir inside it, so I don't get confused with the files. I hope it may be useful!

like image 781
João Daniel Avatar asked Jan 24 '12 12:01

João Daniel


People also ask

Can you read a zipped file into R?

Read Zipped Files into R In the age of big data, it’s not uncommon to encounter a large zipped file of multiple text files. Unzipping will take time. It turns out we can read them into R without unzipping first.

What is the best way to store data in R?

So those files can be stored in various formats. It may be stored in .txt (tab-separated value) file, or in a tabular format i.e .csv (comma-separated value) file or it may be on internet or cloud. R provides very easier methods to read those files. One of the important formats to store a file is in a text file.

How to use zip () and unzip () and tar () functions in R?

How to Use zip (), unzip () and tar (), untar () Functions in R. 1 To zip file, use zip (). 2 To unzip the file, use unzip (). 3 To tar the file, use tar (). 4 To untar the file, use untar ().

How do I read a file with decimal points in R?

read.delim (): This method is used for reading “tab-separated value” files (“.txt”). By default, point (“.”) is used as decimal points. read.delim (file, header = TRUE, sep = “t”, dec = “.”, …) file: the path to the file containing the data to be read into R.


1 Answers

Another solution using unz:

read.zip <- function(file, ...) {   zipFileInfo <- unzip(file, list=TRUE)   if(nrow(zipFileInfo) > 1)     stop("More than one data file inside zip")   else     read.csv(unz(file, as.character(zipFileInfo$Name)), ...) } 
like image 93
Joshua Ulrich Avatar answered Sep 29 '22 19:09

Joshua Ulrich