From murdoch at stats.uwo.ca  Fri Jul 25 13:53:59 2008
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Fri, 25 Jul 2008 07:53:59 -0400
Subject: [Rd] head() for files
In-Reply-To: <F189E18BBAA8B6479F618B9044CF4E9C686C1337E2@owa.bfro.uni-lj.si>
References: <F189E18BBAA8B6479F618B9044CF4E9C686C1337E2@owa.bfro.uni-lj.si>
Message-ID: <4889BED7.8040402@stats.uwo.ca>

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



[Rd] head() for files
Duncan Murdoch