-----Original Message-----
From: Liaw, Andy [mailto:andy_liaw at merck.com]
Sent: Tuesday, October 02, 2001 10:30 AM
To: 'Robert Gentleman'
Cc: r-help (E-mail)
Subject: An example (was RE: [R] file connection, while, readLines and
bro wser)
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
-----Original Message-----
From: Robert Gentleman [mailto:rgentlem at jimmy.harvard.edu]
Sent: Tuesday, October 02, 2001 9:50 AM
To: Liaw, Andy
Subject: Re: [R] file connection, while, readLines and browser
On Tue, Oct 02, 2001 at 09:28:49AM -0400, Liaw, Andy wrote:
Dear R-help,
I have one more question about the functions mentioned in
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
"NA where logical needed". I inserted a browser() right
loop and have it step through the function. It seems to be
first line of the file and never move down. This behaviour
from the call without the browser(). Anyone has ideas why
It seems to work for me. Are you sure that's what you did?
not to edit anything, get a simple example that shows the error and
post that. It makes for a lot less work for those of us that are
willing to look at it and it makes sure that we find the
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
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To:
r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._._._._._._
--
+-------------------------------------------------------------
--------------+
| Robert Gentleman phone : (617) 632-5250
|
| Associate Professor fax: (617) 632-2444
|
| Department of Biostatistics office: M1B28
| Harvard School of Public Health email:
rgentlem at jimmy.dfci.harvard.edu |
+-------------------------------------------------------------
--------------+
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-.-.-
r-help mailing list -- Read