Skip to content

simple if...else causes syntax error

8 messages · Uwe Ligges, Dimitris Rizopoulos, Gabor Grothendieck +4 more

#
I am trying to do the simplest thing in the world.  The following works:

aaa <- ifelse(aaa==5, 6, 7)            
            
But if I want to change the if...else syntax instead, it gives errors
and assigns 7 to aaa.  Here is the problem code:

aaa <- 5
if ( aaa==5 ) { 
   aaa <- 6
}
else {
   aaa <- 7
}
            
Here is the output:
+             aaa <- 6
+   }
Error: syntax error
Error: syntax error
Hope someone can solve this easy question for me.

BTW, how come "?if" does not pull up the help file for the 'if' statement?

Thanks,

Roger
#
roger bos wrote:

            
Parser, try ?"if"


And in ?"if" read the Details section, which tells you:

"[...] In particular, you should not have a newline between } and else 
to avoid a syntax error [...]".


The point is that
    if(A)
        B
is already syntactically complete (else can be omitted), so what the 
parser does not know what follows and has to evaluate ...


Uwe Ligges
#
from the help page of ?"if" (not "?if") you get:

Note that it is a common mistake to forget to put braces `({..})' 
around your statements, e.g., after `if(..)' or `for(...)'. In 
particular, you should not have a newline between `}' and `else' to 
avoid a syntax error ...

So you should use:

aaa <- 5
if ( aaa==5 ) {
   aaa <- 6
} else {
   aaa <- 7
}


Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "roger bos" <roger.bos at gmail.com>
To: <R-help at stat.math.ethz.ch>
Sent: Monday, March 07, 2005 4:16 PM
Subject: [R] simple if...else causes syntax error
#
Jan T. Kim <jtk <at> cmp.uea.ac.uk> writes:

:
: On Mon, Mar 07, 2005 at 10:16:50AM -0500, roger bos wrote:
: > I am trying to do the simplest thing in the world.  The following works:
: > 
: > aaa <- ifelse(aaa==5, 6, 7)            
: >             
: > But if I want to change the if...else syntax instead, it gives errors
: > and assigns 7 to aaa.  Here is the problem code:
: > 
: > aaa <- 5
: > if ( aaa==5 ) { 
: >    aaa <- 6
: > }
: > else {
: >    aaa <- 7
: > }
: 
: This is due to R's (somewhat peculiar) semantics of newline, which R
: interprets as a terminator if an expression can terminate at the position
: of the newline, or else as a plain whitespace, see section on "Separators"
: in the R Language Definition. In the construction
: 
:     if ( aaa==5 ) {
:        aaa <- 6
:     }
: 
: R decides that the final newline can be a terminator, namely of an if-
: expression without an else branch. So, the if-expression is consumed
: by the parser and "forgotten" for the purpose of associating the else
: branch with it. The else branch thus appears to be astray and is reported
: as a syntax error.
: 
: All this does not happen if the entire construct is enclosed in braces.
: Alternatively, the "else" can be placed on one line with the brace closing
: the if branch.
: 
: Out of personal interest: Does anyone here know why the R parser was
: designed this way? Personally, I have been coding in R for years in the
: belief that newline is whitespace, and never even noticed any problems
: because all my ifs with elses were within functions and thus enclosed
: in curly braces.
: 

If it did not work that way it would require console lookahead.  That is 
it would not know that the if statement was finished and would have
to wait for the following statement to be completely typed in before
it could process the if.  The way it works now the if statement can
be processed immediately.
#
On Mon, 7 Mar 2005, roger bos wrote:

            
Other people have told you how to fix this.  I will point out in addition 
that if...else is not different syntax for ifelse() but a very different 
construct.

ifelse() is a function that operates on vectors and returns a vector that 
is always the same length as the first argument. It does not change the 
flow of execution: all three of the arguments are evaluated.

if(){} else {} chooses which branch of code to evaluate based on a single 
logical value.  The value returned by this expression could be of 
completely different length and type depending on which code was 
evaluated.


It might also be worth noting that the behaviour of newlines in 
terminating if() {} expressions is unavoidable in an interpreter using 
this syntax. When the user types

if(condition){
    some.code()
}

the interpreter cannot possibly tell whether an `else' clause is coming. 
Avoiding the problem would require a fairly significant change to the 
language, not just to the parser, eg adding an endif (the shell script 
solution), or requiring parentheses around the whole expression (the LISP 
solution, and in a sense the Python solution, though there the parentheses 
are invisible)



 	-thomas
#
On Mon, Mar 07, 2005 at 10:16:50AM -0500, roger bos wrote:
This is due to R's (somewhat peculiar) semantics of newline, which R
interprets as a terminator if an expression can terminate at the position
of the newline, or else as a plain whitespace, see section on "Separators"
in the R Language Definition. In the construction

    if ( aaa==5 ) {
       aaa <- 6
    }

R decides that the final newline can be a terminator, namely of an if-
expression without an else branch. So, the if-expression is consumed
by the parser and "forgotten" for the purpose of associating the else
branch with it. The else branch thus appears to be astray and is reported
as a syntax error.

All this does not happen if the entire construct is enclosed in braces.
Alternatively, the "else" can be placed on one line with the brace closing
the if branch.

Out of personal interest: Does anyone here know why the R parser was
designed this way? Personally, I have been coding in R for years in the
belief that newline is whitespace, and never even noticed any problems
because all my ifs with elses were within functions and thus enclosed
in curly braces.

Best regards, Jan
#
Thanks to everyone who took the time to point out the simple error. 
You would not think I have been programming in R/S for over a year. 
Could it be that the code works when enclosed in a function?  I tested
my code and it worked previously, but I was running it line by line
and discovered my syntax error.

Thanks again.

Roger



On Mon, 7 Mar 2005 08:36:50 -0800 (PST), Thomas Lumley
<tlumley at u.washington.edu> wrote:
#
Gabor Grothendieck <ggrothendieck at myway.com> writes:
Or put differently, the language designers were not quite aware of the
reason that other command-line languages tend to use explicit
construct terminators like "endif" or "fi".... (I think that is almost
a quote from one of the prominent language experts inside R core, but
I can't pinpoint the details.)