Skip to content

An example (was RE: [R] file connection, while, readLines and bro wser)

1 message · Liaw, Andy

#
Prof. Gentleman (and R-help),

Here's an example of what didn't work.  I still don't understand why.

Function:
trycon <- function(file, n) {
  f.con <- file(file, open="rt")
  on.exit(close(f.con))
  i <- 0
  while( length(readln <- readLines(f.con, 1)) > 0 ) {
    x <- unlist(strsplit(readln, " "))
    if(length(x) <= 6 && x[3] == x[4]) next
    i <- i + 1
  }
  return(invisible())
}

Data file:
1 0.00 ABC ABC 1.00 0 
2 0.00 DEF DEF 1.00 0 CDE 1.00 0 XYZ 0.72 0 
3 0.00 abc abc 1.00 0 ghi 1.00 0 stu 0.72 0 lmn 0.80 0 

R call:
trycon("C:/home/data.txt", 4)
Error in while (length(readln <- readLines(f.con, 1)) > 0) { : 
	missing value where logical needed

The "if(...) next" line seems to be the source of the problem, but the error
message and the behaviour of browser() really throw me off.  Can you shed
some light on this?  (I have a hunch that I'm missing something *real*
obvious...)

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._