[R] unexpected behavior from gzfile and unz

From: Matthew Suderman <matthewsuderman_at_yahoo.com>
Date: Wed, 19 Dec 2007 10:43:45 -0800 (PST)


I get unexpected behavior from "readLines()" and "scan()" depending on how the file is opened with "gzfile" or "unz". More specifically:

> file <- gzfile("file.gz")
> readLines(file,1)

[1] "a\tb\tc"
> readLines(file,1)

[1] "a\tb\tc"
> close(file)

It seems that the stream is rewound between calls to readLines. The same is true if I replace readLines with scan.

However, if I set argument 'open="r"', then rewinding does not occur:

> file <- gzfile("file.gz",open="r")
> readLines(file,1)

[1] "a\tb\tc"
> readLines(file,1)

[1] "1\t2\t3"
> close(file)

Once again, I get the same behavior for scan. The rewinding behavior just described also appears if I open a zip file with "unz".

> file <- unz("file.zip", "file.txt")
> readLines(file,1)

[1] "a\tb\tc"
> readLines(file,1)

[1] "a\tb\tc"
> close(file)

If I add the 'open="r"' argument to the call then I get an error from readLines:

> file <- unz("file.zip", "file.txt", open="r")
> readLines(file,1)

Error in readLines(file, 1) : seek not enabled for this connection
> close(file)

> file <- unz("file.zip", "file.txt", open="rb")
> readLines(file,1)

Error in readLines(file, 1) : seek not enabled for this connection
> close(file)

However, if I instead use "scan" to read the file, then there are no errors and I get the rewind/no rewind behavior described above.

> file <- unz("file.zip", "file.txt")
> scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
> scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
> close(file)

> file <- unz("file.zip", "file.txt", open="r")
> scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
> scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "1" "2" "3"
> close(file)

Is this a bug?

Matt



Be a better friend, newshound, and

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 19 Dec 2007 - 18:50:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 19 Dec 2007 - 20:30:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.