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? Thanks, Simon. -- Dr. Simon Brown Climate extremes research manager simon.brown at metoffice.com http://www.hadleycentre.com Telephone: +44 (0)1392 886879 Fax: +44 (0)870 900 5050 Met Office Hadley Centre, FitzRoy Road, EXETER, EX1 3PB, U.K
In the second case, R stops when it has a syntactically complete clause: if (...) { ... } is complete since an else{} clause is not required. R evaluates it, then moves onto else { ... } which is a syntax error (since it "doesn't have an if {} in front of it, since that has already been evaluated) One way to see this illustrated is to enter these commands a line at a time in interactive mode. I don't know exactly where this appears in the documentation, probably someone will point to it in another message. On Thu, 13 Nov 2003, Brown, Simon wrote:> 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? > > Thanks, > > Simon. >-- 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
"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".] -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
> I'm trying to grasp this: if you're saying (or are you saying) thatthe> only way to have if() know that an else will be present is to put iton the> same line as the closing curly brace of the if() (compound)statement. But> if I look at some code from, e.g., aov and lm, I see plentyviolations of> that rule.There are no violations to that rule in either lm or aov. It simply looks that way if you type lm or aov from within R. If you looked at the functions themselves using an editor, you would see that they all conform to if{ ... }else{ ... }> regards, > PaulJim James W. MacDonald Affymetrix and cDNA Microarray Core University of Michigan Cancer Center 1500 E. Medical Center Drive 7410 CCGC Ann Arbor MI 48109 734-647-5623
Prof Brian Ripley <ripley at stats.ox.ac.uk>> On Fri, 14 Nov 2003, Paul Lemmens wrote: > > > I'm trying to grasp this: if you're saying (or are you saying) that the > > only way to have if() know that an else will be present is to put it on the > > same line as the closing curly brace of the if() (compound) statement. But > > if I look at some code from, e.g., aov and lm, I see plenty violations of > > that rule. > > The actual rule is given in my reference (the one that Ben Bolker did not > bother to look up) earlier in this thread. > > You need to ensure that the code is syntactically incomplete when `else' > is encountered. That will always be true inside a braced expression such > as the bodies of the functions you quote. But at top-level, you do need > to write > > if(condition) something else something_else > > or > > if(condition) { > } else { > } > > since > > if(condition) { > } > else { > } > > fails the test. >BUT, just to make it even clearer, what does succeed interactively is (what is effectively the same as a function body): { if(condition) { "TRUE" } else { "FALSE" } } because the parser has to find the final closing brace before the syntactic item is complete. Ray Brownrigg