[R] Zipping Rdata Files

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Apr 8 08:33:34 CEST 2005


On Thu, 7 Apr 2005 Brian.J.GREGOR at odot.state.or.us wrote:

> Saving Rdata files in a zip archive form can in some cases save a
> considerable amount of disk space.

Not if they were saved with compress=TRUE: it is likely to increase the 
size of compressed saved images.

> R has the zip.file.extract function to extract files from zip archives, 
> but appears not to have any corresponding function to save in zipped 
> form. (At least I have not been able to find anything in the help files 
> or through searching the mail archives.)

That is true, but I think you are looking for gzip format.  If you want 
zip format, just use a system call to zip (if you have it). 
zip.file.extract is provided only because R for Windows needs to unzip on 
systems without unzip.  (On other platforms it calls unzip.)

> The system function can be used to call gzip or some other utility, but 
> perhaps there is a more direct method.

Yes, for gzip (not zip).  gzfile() connections, as used by 
save(compress=TRUE) and by load().

> Also, when I use gzip to zip a file, I get an error message when using

That's because gzip >g<zips a file, not zips a file.  gzip and zip are 
different formats.

> zip.file.extract to extract the file as follows:
> 	> save(trips, file="trips.Rdata")
> 	> system("gzip trips.Rdata")  # saves trips.Rdata in an archive
> named trips.Rdata.gz
> 	> load(zip.file.extract("trips.Rdata", "trips.Rdata.gz"))
> 	[1] "trips.Rdata"
> 	Warning message:
> 	error 1 in extracting from zip file
> Setting options(unzip="gunzip") or options(unzip="gunzip.exe") does not
> solve the error.
> 	> load(zip.file.extract("trips.Rdata", "trips.Rdata.gz"))
> 	Error in open.connection(con, "rb") : unable to open connection
> 	In addition: Warning message:
> 	cannot open compressed file `trips.Rdata'
>
> Of course I could reverse the process with,
> 	system("gunzip trips.Rdata.gz")
> 	load("trips.Rdata")
> but perhaps there is a simpler solution.
>
> P.S. I'm running R 2.0.1 on a Windows XP computer.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list