[R] RE: problem with while loop with next

Liaw, Andy andy_liaw at merck.com
Tue Oct 2 22:20:36 CEST 2001


Prof. Tierney,

Thanks very much for the info.  

Why does the loop work if I move the assignment out of the condition?  E.g.,
the following works:

i <- 0
while(i < 5) {
  i <- i + 1
  if(i < 3) next
  print(i)
}

Regards,
Andy

> -----Original Message-----
> From: Luke Tierney [mailto:luke at nokomis.stat.umn.edu]
> Sent: Tuesday, October 02, 2001 3:36 PM
> To: Liaw, Andy
> Cc: r-help (E-mail)
> Subject: Re: problem with while loop with next (was RE: [R] file
> connection, w hile, readLines and browser)
> 
> 
> It does look like `next' does not work properly with while loops in
> versions through 1.3.1.  We did some reorganizing of looping internals
> in the development branch and that fixed the problem, so it will be
> fixed in 1.4.
> 
> luke
> 
> On Tue, Oct 02, 2001 at 02:54:16PM -0400, Liaw, Andy wrote:
> > Dear R-help,
> > 
> > I think I have kinda isolated the problem I had to the following:
> > 
> > i <- 0
> > while( {i <- i + 1} < 5) {
> >   if(i < 3) next
> >   print(i)
> > }
> > 
> > This seems to go into an infinite loop.  After I break the 
> execution, i has
> > the value 1.
> > 
> > At the R prompt, if I start from i <- 0 and keep typing {i 
> <- i + 1} < 5, it
> > eventually evaluate to TRUE.  So why does the while loop not work?
> > 
> > (BTW, the same while loop runs as I expected in Splus 6.0.3 
> on WinNT.)
> > 
> > Any hints?  I'd really appreciate the help!
> > 
> > Cheers,
> > Andy
> > 
> > 
> > > -----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 
> > > > 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?
> > > > > 
> > > > 
> > > >  It seems to work for me. Are you sure that's what you did? 
> > > It is best
> > > >  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 
> > > problem that
> > > >  you are having.
> > > > 
> > > > 
> > > > > 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
> > > > > 
> > > > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> > > > _._._._._._._._._
> > > > 
> > > > -- 
> > > > +-------------------------------------------------------------
> > > > --------------+
> > > > | 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 
> > 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
> > 
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._.
> > _._
> > 
> > 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> > 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
> > 
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> 
> -- 
> Luke Tierney
> University of Minnesota                      Phone:           
> 612-625-7843
> School of Statistics                         Fax:             
> 612-624-8868
> 313 Ford Hall, 224 Church St. S.E.           email:      
> luke at stat.umn.edu
> Minneapolis, MN 55455 USA                    WWW:  
http://www.stat.umn.edu
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._

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