Skip to content

R parser for If-else

6 messages · dani, robin hankin, Wacek Kusnierczyk +2 more

#
Hi list,

I don't know if somebody has spent a lot of time debugging strange
problems with if else positioning - the parser seems to recognize only
the syntax bellow - this is the only way of making these pieces of
code to work.

As far as i'm concerned, no examples were available (it would be so
awesome to have them in the introductory manual!)

#Try to change the placement of the keywords and you are dead! 4 examples
Ex1:
if (1==1){
?print('if')
?print('if again')
?}else
?print('else')

Ex2:
if (2==2) print('if') else print('else')

Ex3:
if (2==2){
?print('if')
?print('if again')
?}else
?{
?print('else')
?print('else2')
?}

Ex4:
if (2==2){
?print('if')
?print('if again')
}else print('else')



cheers,
-------------------------------------
Daniela
#
D> Hi list,
    D> I don't know if somebody has spent a lot of time debugging strange
    D> problems with if else positioning - the parser seems to recognize only
    D> the syntax bellow - this is the only way of making these pieces of
    D> code to work.

    D> As far as i'm concerned, no examples were available (it would be so
    D> awesome to have them in the introductory manual!)

    D> #Try to change the placement of the keywords and you are dead! 
["dead"?] 

Oh dear... 
Note this has nothing to do with   if( ) .. else ..
but indeed with how things are parsed.

I think this is FAQ (or should become one):

?if [the help page you really should read before spending too
     much time or even post to R-help]  
has the following section

 >      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 in entering a 'if ... else'
 >      construct at the keyboard or via 'source'. For that reason, one
 >      (somewhat extreme) attitude of defensive programming is to always
 >      use braces, e.g., for 'if' clauses.

Regards,
Martin Maechler, ETH Zurich


    D> Ex1:
    D> if (1==1){
    D> ?print('if')
    D> ?print('if again')
    D> ?}else
    D> ?print('else')

    D> Ex2:
    D> if (2==2) print('if') else print('else')

    D> Ex3:
    D> if (2==2){
    D> ?print('if')
    D> ?print('if again')
    D> ?}else
    D> ?{
    D> ?print('else')
    D> ?print('else2')
    D> ?}

    D> Ex4:
    D> if (2==2){
    D> ?print('if')
    D> ?print('if again')
    D> }else print('else')



    D> cheers,
    D> -------------------------------------
    D> Daniela

    D> ______________________________________________
    D> R-help at r-project.org mailing list
    D> https://stat.ethz.ch/mailman/listinfo/r-help
    D> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    D> and provide commented, minimal, self-contained, reproducible code.
#
I too have had many problems with if-else.

My solution is to always always always
use the line

"} else {"

in any if-else construction.  This guarantees that
there won't be problems of the sort discussed here.

HTH

rksh
Martin Maechler wrote:

  
    
#
Martin Maechler wrote:
?if

won't parse completely, you need

    ?'if'

;)

vQ
#

        
WK> Martin Maechler wrote:
>> 
    >> I think this is FAQ (or should become one):
    >> 
    >> ?if [the help page you really should read before spending too
    >> much time or even post to R-help]  
    >> 

    WK> ?if

    WK> won't parse completely, you need

    WK> ?'if'

this depends on your "GUI" for R :
In ESS (Emacs speaks statistics), 

  ?if   does work

[but other non-standard versions of calling '?' do not;
 so I'm not claiming superiority of ESS here (but in general, of
 course, I do :-) ]

Martin
#
On Wed, Feb 25, 2009 at 10:03 AM, Martin Maechler
<maechler at stat.math.ethz.ch> wrote:
does that mean, that

# (A)
if (i==1) {
  cat("i==1")
}
else {
  cat ("i !=1")
}

should be avoided, as it causes an error in a file which will be
called with source(), and

#(B)
if (i==1) {
  cat("i==1")
} else {
  cat ("i !=1")
}

must be used instead of (A)?

but (A) can be used in a function definition in a file which will be
sourced, like:

#(C)
ifC <- function(i) {
  if (i==1) {
    cat("i==1")
  }
  else {
    cat ("i !=1")
  }
}


That might definitely explain some strange unexplainable errors I got.

Rainer