[R] R multiline expression grief

Paul Suckling paul.suckling at gmail.com
Fri Mar 13 14:26:25 CET 2009


I get it. Thanks everyone for the feedback.

Now that I understand how it works, my comment would be that this
system is dangerous since it makes it difficult to read the code and
easy to make errors when typing it. I recognise that this is something
so fundamental that it is unlikely to be changed so I'll have to adapt
to it.

My feeling is that such confusion could be avoided however, by
introducing a line continuation character (or group of characters)
into R that could be used to indicate to the parser (and reader of the
code) that the expression continues onto the next line.

Something like

f <- a ...
  + b ...
  + c

Cheers,

Paul

2009/3/13 Paul Suckling <paul.suckling at gmail.com>:
> 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
>



-- 
Nashi Power.
http://nashi.podzone.org/
Registered address: 7 Trescoe Gardens, Harrow, Middx., U.K.




More information about the R-help mailing list