I get unexpected behavior from "readLines()" and
"scan()" depending on how the file is opened with
"gzfile" or "unz". More specifically:
▾ Quoted text (2 lines)
file <- gzfile("file.gz")
readLines(file,1)
[1] "a\tb\tc"
[1] "a\tb\tc"
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:
▾ Quoted text (2 lines)
file <- gzfile("file.gz",open="r")
readLines(file,1)
[1] "a\tb\tc"
[1] "1\t2\t3"
Once again, I get the same behavior for scan. The
rewinding behavior just described also appears if I
open a zip file with "unz".
▾ Quoted text (2 lines)
file <- unz("file.zip", "file.txt")
readLines(file,1)
[1] "a\tb\tc"
[1] "a\tb\tc"
If I add the 'open="r"' argument to the call then I
get an error from readLines:
▾ Quoted text (2 lines)
file <- unz("file.zip", "file.txt", open="r")
readLines(file,1)
Error in readLines(file, 1) : seek not enabled for
this connection
▾ Quoted text (3 lines)
file <- unz("file.zip", "file.txt", open="rb")
readLines(file,1)
Error in readLines(file, 1) : seek not enabled for
this connection
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.
▾ Quoted text (2 lines)
file <- unz("file.zip", "file.txt")
scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
▾ Quoted text (1 line)
scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
▾ Quoted text (3 lines)
file <- unz("file.zip", "file.txt", open="r")
scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "a" "b" "c"
▾ Quoted text (1 line)
scan(file,nlines=1,sep="\t",what=character(0))
Read 3 items
[1] "1" "2" "3"
Is this a bug?
Matt
____________________________________________________________________________________
Be a better friend, newshound, and
Message-ID:
<112228.85376.qm@web52507.mail.re2.yahoo.com>