[R] R multiline expression grief

Gabor Grothendieck ggrothendieck at gmail.com
Fri Mar 13 14:28:31 CET 2009


If all your code has semicolons you could write a program that
puts each statement on one line based on the semicolons and
then passing it through R will reformat it in a standard way.
See Rtidy.bat in the batchfiles distribution for the reformatting part:
http://batchfiles.googlecode.com

On Fri, Mar 13, 2009 at 8:55 AM, Paul Suckling <paul.suckling at gmail.com> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list