Anita Gulyasne Goldpergel
2002-Nov-06 04:01 UTC
[R] R bug? (if-else problem in main program)
Hi everybody, I've found a very interesting problem in R: the if-else statement doesn't work in a main program. Sounds crazy, but true. I tried this very easy example, and I got syntax error at the "else" line. Example: ------- a <- 1 if ( a == 1 ) print("yes") else print("no") -------- I tried on Windows and on Linux, but none of them works. (Detailed version info at the end of the mail.) Temporally you could fix it in two way: 1. Put it in 1 line: if ( a == 1) print("yes") else print("no") will work. 2. Put it in a function. If you have any idea for better resolve of this problem, please send me an email, I'm not subscribed to the list. Thanks, Anita Version info: 1. Windows: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 1 minor 5.0 year 2002 month 04 day 29 language R 2. Linux: platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 5.1 year 2002 month 06 day 17 language R -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Example: > ------- > a <- 1 > > if ( a == 1 ) > print("yes") > else > print("no") > --------If you want to split this over several lines, you need curly braces: a <- 1 if ( a == 1 ) { print ('yes') } else { print ('no') } Cheers, Lorenz -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This is documented in many places. See the help page for `if', or in fuller detail, section 3.2 of the Language Manual. The solution is to use braces if(a == 1) { print("yes") } else { print("no") } as described in ?"if". On Tue, 5 Nov 2002, Anita Gulyasne Goldpergel wrote:> I've found a very interesting problem in R: the if-else statement doesn't > work in a main program. Sounds crazy, but true.This is not `a main program' but using R interactively at the top (prompt) level. It will work in a function, the only sort of `programs' that exist in R. The help and manuals are your friends! [...] -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I found the same thing. But if you write the same code in a function, there is no problem. E.g., myfunc<-function(a) { if ( a == 1 ) { print("yes") } else { print("no") } } # Or myfunc<-function(a) { if ( a == 1 ) print("yes") else print("no") }> myfunc(5)[1] "no"> myfunc(1)[1] "yes" Therefore, it may be a good idea to write functions for specific tasks from programming point of view. Hope this is also helpful. -Fasheng -----Original Message----- From: Anita Gulyasne Goldpergel [mailto:anitag at latte.harvard.edu] Sent: Wednesday, November 06, 2002 2:22 PM To: Lorenz Gygax Cc: r-help at stat.math.ethz.ch Subject: [R] Re: R bug? (if-else problem in main program) Thank you very much everybody for your answers! Before I wrote my message, I had tried to use brackets. So I've discovered something, what can be useful for everyone: If I put the end-bracelet in different line from else, it doesn't work: if ( a == 1 ) { print("yes") } else { print("no") } But if I put it one line, as you wrote, like } else { it works. Thanks again, Anita Lorenz Gygax writes:> > >> Example: >> ------- >> a <- 1 >> >> if ( a == 1 ) >> print("yes") >> else >> print("no") >> -------- > > If you want to split this over several lines, you need curly braces: > > a <- 1 > > if ( a == 1 ) { > print ('yes') > } else { > print ('no') > } > > Cheers, Lorenz > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._