Decompress gz file using R

Here is a worked example that may help illustrate what gzfile() and gzcon() are for

foo <- data.frame(a=LETTERS[1:3], b=rnorm(3))
foo
#  a        b
#1 A 0.586882
#2 B 0.218608
#3 C 1.290776
write.table(foo, file="/tmp/foo.csv")
system("gzip /tmp/foo.csv")             # being very explicit

Now that the file is written, instead of implicit use of file(), use gzfile():

read.table(gzfile("/tmp/foo.csv.gz"))   
#  a        b
#1 A 0.586882
#2 B 0.218608
#3 C 1.290776

The file you point is a compressed tar archive, and as far as I know, R itself has no interface to tar archives. These are commonly used to distribute source code–as for example for R packages and R sources.

Leave a Comment