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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sun, 20 Apr 2008 19:04:33 +0100 (BST)

On Sun, 20 Apr 2008, Stephen Eglen wrote:

> 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?

gzfile is not a text-mode connection, and the help is wrong for those. E.g.

> readLines(pipe("gzip -dc short.gz"))

[1] "7" "5"

does the same. It is only text-mode blocking connections which do this.

(I just looked at the code, and you might have found that illuminating.)

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
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 - 18:08:12 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 - 20:30:56 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