[R] Problem with parser and if/else
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Thu Nov 13 14:55:24 CET 2003
On Thu, 13 Nov 2003, Ben Bolker wrote:
>
> 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.
help("if"), for example (the most obvious place to look?)
MASS4, p.58
....
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
--
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
More information about the R-help
mailing list