[R] syntax rules
Spencer Graves
spencer.graves at pdf.com
Tue Feb 25 17:35:06 CET 2003
Permit me to elaborate on Andy Liaw's brief comments:
Liaw, Andy wrote:
>>From: Hiroyuki Kawakatsu [mailto:kawa at aris.ss.uci.edu]
>>
>>hi,
>>
>>i lost half a day trying to figure out how r is parsing statements
>>in multiple lines. can someone explain (or direct me to
>>documentation) the
>>following. consider the following statements in a program file, say
>>foo.r:
>>
>>a <- 1 +
>> 2;
"a <- 1 +" is NOT systactically complete. Therefore, it looks for more
of this statement on the next line.
>>b <- {1
>> + 2};
"b <- {1" is syntactically complete: The "{" opens a separate
evaluation (frame?), the first statement of which is "1". This
evaluation also includes a second "statement", which is "+2". The
variable "b" gets the value of last statement in the frame, which is 2.
If you start a line with "(", then it will not be syntactically complete
until it finds the matching ")". Consider the following:
> (b <- 1
+ +2)
[1] 3
>
> The first line is not a complete statement because there's no enclosing }.
> Thus b gets assign the value of the statement enclosed in {}.
>
>
>>{c <- 1
>> + 2};
>
>
> The first line here *is* a complete statement. The entire { } expression
> gets the value of the the last line, namely "+2". Try:
>
>
>>junk <- { b <- 1
>
> + + 2};
>
>>b
>
> [1] 1
>
>>junk
>
> [1] 2
>
> Andy
>
>
>
>>d <- c(1,
>> 2);
>>
"c" is a function that creates a vector or a list, in this case, the
vector consisting of "1" and "2".
>>if i do source("foo.r"), i get a=3, b=2, c=1, d={1,2}.
>>according to the r language definition p.11, section 3.2, it says
>>
>>"A semicolon always indicates the end of a statement while a new line
>>'may' indicate the end of a statement. If the current statement is not
>>syntactically complete new lines are simply ignored by the evaluator."
Here, the documentation "new lines are simply ignored" might be clearer
as "new line characters are simply ignored".
a <- 1; b <- 2
is two statements, on one line, while
a <- 1 +
2
is one statement on 2 lines; since the first line is not syntactically
complete, the "new line [character]" that ends the first line is ignored.
>>
>>then, a and d are evaluated as expected since the first lines are not
>>syntactically complete. however, why does b evaluate to 2 and c to 1?
>>(it appears to evaluate differently if i do this interactively.)
>>i got the idea of using curly braces from p.12 of the
>>language definition.
>>
>>is there a way to keep adding terms with a line beginning
>>with a plus sign
>>(notationally, i don't like plus symbols hanging at the end
>>of a line...)?
>>
>>h.
>>--------------------------------------------------------------------
>>Time series regression studies give no sign of converging toward the
>>truth. (Phillip Cagan)
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>
>
>
>
> ------------------------------------------------------------------------------
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
More information about the R-help
mailing list