[Rd] head() for files

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jul 25 13:53:59 CEST 2008


On 25/07/2008 7:30 AM, Gorjanc Gregor wrote:
> Hi!
> 
> I was thinking about using the head and tail functions also for files i.e.
> show me the first/last few lines of a file. For example:
> 
> zz <- file("ex.data", "w")  # open an output file connection
> cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
> cat("One more line\n", file = zz)
> close(zz)
> readLines("ex.data")
> 
> head.character <- function(x, n=6, ...) readLines(con=x, n=n, ...)
> 
> head.character("ex.data")
> 
> As can be seen, this is essentially a wrapper for readLines, but a handy one.
> Of course, S3 "linking" is needed, perhaps also for class "connection".
> 
> I am not prefectly satisfied with it as I would like to see printout line by line.
> Any ideas?

I think you should have called it head.connection, and then

head(file("ex.data"))

would sort of work.  The problems with this are open connections:  what 
should it do on one of those?  Seeking to the start would make sense, 
but not all connections can do that.

Duncan Murdoch



More information about the R-devel mailing list