[R] Is there any difference between <- and =

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu Mar 12 21:48:04 CET 2009


Jens Oehlschlägel wrote:
> Sean,
>
>   
>> would like to receive expert opinion to avoid potential trouble
>>     
> [..]
>   
>> i think the following is the most secure way if one really
>> really has to do assignment in a function call
>>    f({a=3})
>> and if one keeps this convention, <- can be dropped altogether.
>>     
>
> secure is relative, since due to R's lazy evaluation you never know whether a function's argument is being evalutated, 

for sure, but that's not different between <- and {=}.  in both cases,
if the argument is not used, assignment will not be made.

>> f<- function(x)TRUE
>> x <- 1
>> f((x=2)) # obscured attempt to assign in a function call
>>     
> [1] TRUE
>   
>> x
>>     
> [1] 1
>   

same with f(x<-2)

> Thus there is dangerous advice in the referenced blog which reads:
> "
> f(x <- 3)
> which means "assign 3 to x, and call f with the first argument set to the value 3
> "
> This might be the case in C but not in R. Actually in R "f(x <- 3)" means: call f with a first unevaluated argument "x <- 3", and if and only if f decides to evaluate its first argument, then the assignment is done. 

indeed.  i have already argued that using assignment within function
calls is not a good idea; but pick any book on r, you likely to find
numerous examples of it, without an appropriate warning.


<snip>

>
> Here it is unpredictable whether your assignment takes place. Thus assigning like f({x=1}) or f((x=1))

or f(x<-1)

> is the maximum dangerous thing to do: even if you have a code-reviewer and the guy is aware of the danger of f(x<-1) he will probably miss it because f((x=1)) does look too similar to a standard call f(x=1).
>   

sorry, to me f({x=1}) looks pretty much unlike f(x=1).  if someone uses
assignments in function calls and does not know what he's doing, it's
his fault.

> According to help("<-"), R's assignment operator is rather "<-" than "=":
>
> "
> The operators <- and = assign into the environment in which they are evaluated. 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.
>   

to me, it doesn't say anything like that R's assignment operator is
rather "<-" than "=".


> "
>
> So my recommendation is 
> 1) use R's assignment operator with two spaces around (or assign()) and don't obscure 

... but do beautify ...

> assignments by using C's 

don't acknowledge c for it

> assignment operator (or other languages equality operator)
> 2) do not assign in function arguments unless you have good reasons like in system.time(x <- something)
>   

certainly good advice.


>
> P.P.S. a puzzle, following an old tradition:
>
> What is going on here? (and what would you need to do to prove it?)
>   

complete mystery to me.

cheers,
vQ




More information about the R-help mailing list