Skip to content
Prev 31421 / 398506 Next

Scanning data files line-by-line

Hi all, thanks to everyone again for helping out.  I don't want to
generate too many messages, but this problem seems common enough that
maybe it's worth a summary.

What I can do is this.  Let's say "file" has lines of double, string,
double with variable number of spaces between fields followed by EOF.

aaa <- file( "file", "r" )

while( length( ( x <- scan( aaa, nlines = 1, list( 0, "", 0 ) ) )[[1]] )
 > 0 )
{
   check to see if x is empty again (by length( x[[1]] ) > 0 ) since
   we would read in the EOF character into x still

   if not empty
      start processing
}

close( aaa )

Here x is a list and x[[1]] is the first field, etc.

Professor Ripley also suggested textConnections, but I didn't
experiment -- I'm usually happy to find something that works.  :-)

Thanks again.