[R] R parser for If-else
Rainer M Krug
r.m.krug at gmail.com
Wed Feb 25 10:55:10 CET 2009
On Wed, Feb 25, 2009 at 10:03 AM, Martin Maechler
<maechler at stat.math.ethz.ch> wrote:
>>>>>> "D" == Dani <danicyber at gmail.com>
>>>>>> on Tue, 24 Feb 2009 14:09:36 -0800 writes:
>
> 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.
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
>
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)
Centre of Excellence for Invasion Biology
Faculty of Science
Natural Sciences Building
Private Bag X1
University of Stellenbosch
Matieland 7602
South Africa
More information about the R-help
mailing list