[R] file connection, while, readLines and browser

Liaw, Andy andy_liaw at merck.com
Tue Oct 2 15:28:49 CEST 2001


Dear R-help,

I have one more question about the functions mentioned in the subject.
Again, the system info is:
platform i386-pc-mingw32
arch     x86            
os       Win32          (OK, it's NT4sp6)
system   x86, Win32     
status                  
major    1              
minor    3.1            
year     2001           
month    08             
day      31             
language R              

I defined a function like the following:

myfun <- function(file, maxline) {
  f.con <- file(file, open="rt")
  on.exit(close(f.con))
  while(length(readln <- readLines(f.con, 1)) > 0 ) {
    ## Do something with readln...
  }
  ## Do something more...
}

This didn't work for me.  It gave me an error at the while statement, saying
"NA where logical needed".  I inserted a browser() right inside the while
loop and have it step through the function.  It seems to be stuck at the
first line of the file and never move down.  This behaviour is different
from the call without the browser().  Anyone has ideas why this is the case?

What had worked for me is something like the following:
  repeat {
    readln <- readLines(f.con, 1)
    if(length(readln) == 0) break
    ## do something...
  }

Any suggestions are greatly appreciated.

Regards,
Andy

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list