Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the "=" R operator should not be used in functions?

The manual states:

The operator ‘<-’ can be used anywhere, whereas the operator ‘=’ is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

The question here mention the difference when used in the function call. But in the function definition, it seems to work normally:

a = function () 
{
    b = 2
    x <- 3
    y <<- 4
}

a()
# (b and x are undefined here)

So why the manual mentions that the operator ‘=’ is only allowed at the top level??

There is nothing about it in the language definition (there is no = operator listed, what a shame!)

like image 580
Tomas Avatar asked Jun 08 '12 13:06

Tomas


1 Answers

The text you quote says at the top level OR in a braced list of subexpressions. You are using it in a braced list of subexpressions. Which is allowed.

You have to go to great lengths to find an expression which is neither toplevel nor within braces. Here is one. You sometimes want to wrap an assignment inside a try block: try( x <- f() ) is fine, but try( x = f(x) ) is not -- you need to either change the assignment operator or add braces.

like image 114
Vincent Zoonekynd Avatar answered Sep 21 '22 01:09

Vincent Zoonekynd