[R] How to read last (incomplete) line from gzfile()?

From: Stephen Eglen <S.J.Eglen_at_damtp.cam.ac.uk>
Date: Sun, 20 Apr 2008 18:35:07 +0100


Hi,

I have some text files that do not have trailing \n on the last (incomplete) line; how do I read in the last line?

e.g. here is a test case: [linux + R version 2.6.1 (2007-11-26) ]

z <- gzfile("short.gz", open="w")
cat("7\n5\n3", file=z)
close(z)

z <- gzfile('short.gz')
readLines(z)

[1] "7" "5"

readLines would indicate that for blocking connections (which I assume this is), the last line (containing 3) would be read in, and a warning would be generated. I get the desired behaviour if I use a file() on a file that is not compressed:

y <- file("short", open="w")
cat("7\n5\n3", file=y)
close(y)

y <- file('short')
readLines(y)

[1] "7" "5" "3"
Warning message:
In readLines(y) : incomplete final line found on 'short'

What am I missing?

Thanks, Stephen



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 Sun 20 Apr 2008 - 17:38:57 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 Sun 20 Apr 2008 - 19:30:30 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.

list of date sections of archive