Skip to content

Is there any difference between <- and =

6 messages · Jens Oehlschlägel, Wacek Kusnierczyk, David Smith +2 more

#
Sean,
[..]
secure is relative, since due to R's lazy evaluation you never know whether a function's argument is being evalutated, look at:
[1] TRUE
[1] 1

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. To make this very clear:
[1] TRUE
[1] 2
[1] 3
[1] TRUE
[1] 4
[1] 5
[1] TRUE
[1] 6
[1] TRUE

Here it is unpredictable whether your assignment takes place. Thus assigning like 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).

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.
"

So my recommendation is 
1) use R's assignment operator with two spaces around (or assign()) and don't obscure assignments by using C's 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)

HTH


Jens Oehlschl?gel

P.S. Disclaimer: you can consider me biased towards "<-", never trust experts, whether experienced or not.

P.P.S. a puzzle, following an old tradition:

What is going on here? (and what would you need to do to prove it?)
[1] ".GlobalEnv"        "package:stats"     "package:graphics"  "package:grDevices" "package:utils"     "package:datasets"  "package:methods"  
[8] "Autoloads"         "package:base"
[1] "y"
[1] 1 2 3
[1] TRUE
[1] 1 2 3
[1] 2 2 2
_                           
platform       i386-pc-mingw32             
arch           i386                        
os             mingw32                     
system         i386, mingw32               
status                                     
major          2                           
minor          8.1                         
year           2008                        
month          12                          
day            22                          
svn rev        47281                       
language       R                           
version.string R version 2.8.1 (2008-12-22)

--
#
Jens Oehlschl?gel wrote:
for sure, but that's not different between <- and {=}.  in both cases,
if the argument is not used, assignment will not be made.
same with f(x<-2)
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>
or 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.
to me, it doesn't say anything like that R's assignment operator is
rather "<-" than "=".
... but do beautify ...
don't acknowledge c for it
certainly good advice.
complete mystery to me.

cheers,
vQ
#
On Thu, Mar 12, 2009 at 8:29 AM, "Jens Oehlschl?gel" <oehl_list at gmx.de> wrote:
The thrust of the blog post was the stylistic question of whether to
use <- or = for assignment, not a recommendation to use constructs
like this. ?(In fact, the next line reads "This is a contrived example
though, and never really occurs in real-world programming.")  But I've
added your sound advice that such constructs are best avoided. I've
never seen the need to perform assignments in function calls before,
but I'll avoid them now knowing about potential for mischief from lazy
evaluation. Thanks.

http://blog.revolution-computing.com/2008/12/use-equals-or-arrow-for-assignment.html

# David Smith

--
David M Smith <david at revolution-computing.com>
Director of Community, REvolution Computing www.revolution-computing.com
Tel: +1 (206) 577-4778 x3203 (Seattle, USA)
#
I think Venables' and Ripley's convention makes good sense:

http://www.stat.auckland.ac.nz/mail/archive/r-downunder/2008-October/000300.html

So we not only are explicit about what we are assigning, but where we
are assigning it.

Cheers,

Simon.
On Thu, 2009-03-12 at 17:10 -0700, David M Smith wrote:
#
Sean Zhang wrote:
-1?
this is what you *really* shouldn't do unless you know what you're
doing.  here you have not only the issue of whether the assignment will
actually be made, but also the issue of the order of evaluation of the
argument expressions, which depends on the when the arguments are used
within the function.

    f = function(a, b, c)
       if (c) a-b
       else b-a

    a=1; b=1; f(a <- a+1, b <- a, TRUE)
    # 0
    a
    # 2
    b
    # 2
   
    a=1; b=1; f(a <- a+1, b <- a, FALSE)
    # -1
    a
    # 2
    b
    # 1
i don't think you break lexical scoping here. 

vQ