[R] Problem with parser and if/else

Ben Bolker bolker at zoo.ufl.edu
Thu Nov 13 16:57:35 CET 2003


  With all due respect to BDR and you, I think this behavior is not
obvious to casual/new users (using the R search page with "if else" as the
search string turns up nearly identical queries from 1998, 2001, and
2002).  There's a philosophical issue here, of course, about how much we
need to hold people's hands/fill the help files with details about
behavior that is clearly defined but not obvious to beginners.  (And the
fact that some people don't read help pages anyway ...)

  I don't have the new edition of MASS (shame on me), but looking at both 
p. 93 of MASS 3d ed. and at help("if") in R 1.8.0, I don't see this 
problem clearly highlighted (yes, reading the syntax

if(cond) cons.expr  else  alt.expr

very precisely one could infer that the cons.expr and the alt.expr had to
be "on the same line" syntactically).  There *is* a note in the help page
about forgetting braces.

How much would it hurt to add

Another common mistake is to begin an 'else' clause on a new line; this
causes a syntax error, because R assumes that the preceding 'if' statement 
is finished, and tries to read the 'else' clause as a new statement.  

to the documentation?

On 13 Nov 2003, Peter Dalgaard wrote:

> "Brown, Simon" <simon.brown at metoffice.com> writes:
> 
> > 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?
> 
> Again? This has been hashed over several times before. The basic issue
> is whether a statement can be assumed to be syntactically complete at
> the end of a line. It is fairly obvious what happens when you type the
> same expressions at an interactive prompt:
> 
> > x1 <- 1:6
> > t1 <- 5
> > if (length(x1) >= t1) {
> + cat("in the if 2\n")
> + }
> in the if 2
> > else {
> Error: syntax error
> > cat("in the else\n")
> in the else
> > }
> Error: syntax error
> 
> Notice that the first right curly brace is seen as terminating the if
> construct. Otherwise, R would need to wait and check whether the
> *next* line starts with an "else" which would certainly be confusing
> in interactive use. So R assumes the expression is finished and
> evaluates it. Then it gets an "else" keyword that it doesn't know what
> to do with and barfs.
> 
> Files that are source()'ed are subject to the same restrictions as
> code given as input. This is a fairly useful consistency requirement. 
> 
> [Come to think of it, it is not entirely obvious that we couldn't have
> "else ..." working like "if (TRUE) ..." or "if (FALSE) ..." depending
> on the result of a previous if(). I suppose the issue is how to make
> sure that there actually is a matching "if".]
> 
> 

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