[R] R multiline expression grief
(Ted Harding)
Ted.Harding at manchester.ac.uk
Fri Mar 13 15:16:11 CET 2009
On 13-Mar-09 12:55:35, Paul Suckling 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
The lines are not being ignored! In
e <- a +
b +
c;
each line (until the last) is syntactically incomplete, so the R
parser continues on to the next line until the expression is
complete; and the ";" is irrelevant for this purpose. Unlike C,
but like (say) 'awk', the ";" in R serves to terminate an expression
when this is followed on the same line by another one, so it is
basically a separator.
In
f <- a
+ b
+ c;
however, "f <- a" is complete, so the value of 'a' is assigned
to f. The line "+ b" would have sent the value of 'b' (the "+"
being the unary operator "+" which does not change anything)
to the console if it did not occur inside a function definition.
As it is, although "+ b" is evaluated, because it is inside the
function no putput is produced. Similarly for "+ c;" (and, once
again, the ";" is irrelevant since a ";" at the end of a line
does nothing -- unless the line was syntatically incomplete at
that point, in which case ";" as the expression terminator
would trigger a syntax error since an incomplete expression
was being terminated. So
f <- a
+ b
+ c;
is not a multiline expression. It is three expressions on three
separate lines.
The only suggestion I can make is that you have to change your
"house style" -- it is at odds with the way the R parser works,
and is bound to cause "much grief".
Best wishes, and good luck!
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 13-Mar-09 Time: 14:16:08
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list