[R] R multiline expression grief

Paul Suckling paul.suckling at gmail.com
Fri Mar 13 13:55:35 CET 2009


Dear all.

After much grief I have finally found the source of some weird
discrepancies in results generated using R. It turns out that this is
due to the way R handles multi-line expressions. Here is an example
with R version 2.8.1:

----------------------------------------------------
# R-script...

r_parse_error <- function ()
{
  a <- 1;
  b <- 1;
  c <- 1;
  d <- a + b + c;
  e <- a +
    b +
    c;
  f <- a
    + b
    + c;
  cat('a',a,"\n");
  cat('b',b,"\n");
  cat('c',c,"\n");
  cat('d',d,"\n");
  cat('e',e,"\n");
  cat('f',f,"\n");
}
----------------------------------------------------
> r_parse_error();
a 1
b 1
c 1
d 3
e 3
f 1
----------------------------------------------------

As far as I am concerned f should have the value 3.

This is causing me endless problems since case f is our house style
for breaking up expressions for readability. All our code will need to
be rechecked as a result. Is this behaviour a bug? If not, is it
possible to get R to generate a warning that several lines of an
expression are potentially being ignored, perhaps by turning on a
strict mode which requires the semi-colons?

Thank you,

Paul




More information about the R-help mailing list