[R] reading file in zip archive
Peter Langfelder
peter.langfelder at gmail.com
Thu May 31 00:35:14 CEST 2012
On Wed, May 30, 2012 at 12:47 PM, Iain Gallagher
<iaingallagher at btopenworld.com> wrote:
> Hi Phil
>
> Thanks, but this still doesn't work.
>
>
> Here's a reproducible example (was wrapping my head around these functions before).
>
> x <- as.data.frame(cbind(rep('a',5), rep('b',5)))
> y <- as.data.frame(cbind(rep('c',5), rep('d',5)))
>
> write.table(x, 'x.txt', sep='\t', quote=FALSE)
> write.table(y, 'y.txt', sep='\t', quote=FALSE)
>
> zip('test.zip', files = c('x.txt', 'y.txt'))
>
> pathToZip <- paste(getwd(), '/test.zip', sep='')
>
> z <- unz(pathToZip, 'x.txt', 'r')
> zT <- read.table(z, header=FALSE, sep='\t')
>
> Error in read.table(z, header = FALSE, sep = "\t") :
> seek not enabled for this connection
I get the same error and I don't have direct advice on how to avoid
it, but you can avoid working directly with the zip connection by
first unzipping the file, then reading it in:
pathToZip <- paste(getwd(), '/test.zip', sep='')
file = "x.txt"
command = paste("unzip -f ", pathToZip, file);
system(command)
zT = read.table(file, header = FALSE, sep = "\t")
By the way, I got an error reading the file x.txt since the
write.table command also saved row names. I had to add
row.names = FALSE
to the write table calls to make it work, like this:
x <- as.data.frame(cbind(rep('a',5), rep('b',5)))
y <- as.data.frame(cbind(rep('c',5), rep('d',5)))
write.table(x, 'x.txt', sep='\t', quote=FALSE, row.names = FALSE)
write.table(y, 'y.txt', sep='\t', quote=FALSE, row.names = FALSE)
zip('test.zip', files = c('x.txt', 'y.txt'))
HTH,
Peter
More information about the R-help
mailing list