[R] Problem with parser and if/else

Ben Bolker bolker at zoo.ufl.edu
Thu Nov 13 14:42:01 CET 2003


  
  In the second case, R stops when it has a syntactically complete clause:

if (...) {
  ...
}

is complete since an else{} clause is not required.  R evaluates it, then 
moves onto 

else { ... }

which is a syntax error (since it "doesn't have an if {} in front of it, 
since that has already been evaluated)

  One way to see this illustrated is to enter these commands a line at a 
time in interactive mode.

  I don't know exactly where this appears in the documentation, probably 
someone will point to it in another message.

On Thu, 13 Nov 2003, Brown, Simon wrote:

> Dear r-help people,
> 
> could you confirm that this is correct behaviour for R?  I am using RH9.
> 
> the code:
> x1 <- 1:6
> t1 <- 5
> if (length(x1) >= t1) {
> 	cat("in the if \n")
> } else {
> 	cat("in the else\n")
> }
> 
> runs fine:
> > source("test_if_else.R")
> in the if
> >
> 
> but the code:
> x1 <- 1:6
> t1 <- 5
> if (length(x1) >= t1) {
> 	cat("in the if 2\n")
> } 
> else {
> 	cat("in the else\n")
> }
> 
> fails with the error:
> > source("test_if_else2.R")
> Error in parse(file, n, text, prompt) : syntax error on line 6
> >
> 
> Could someone explain this to me please?
> 
> Thanks,
> 
> Simon.
> 

-- 
620B Bartram Hall                            bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704




More information about the R-help mailing list