0

We have

=

<-

<<-

Can someone explain exactly what they do? If there are any more? When I use <<-, it seems to mess my functions but, but I want to declare things globally so I have them when the function is done (I don't want to return them from the function because I'm optimizing over something else)

wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90
  • Unfortunately, the linked question/answer does not describe `<<-`, so it shouldn't be closed. – Thomas Mar 31 '14 at 05:57
  • Those are assignment operators, not declaration operators/statements. In R, assigning to a variable which does not already exist does cause it to be "declared", but that is a side effect, not the primary effect. – Brian Diggs Mar 31 '14 at 16:38

1 Answers1

5

In some sense = and <- are equivalent, but the latter is preferred because = is also overwritten to specify default arguments (where <- will not work).

As for <<-, it is trickier and not recommended. In R, every step of execution along arbitrary code will be associated with a stack of environments--the current environment, the environment the current function was called from, etc. The operator <<- attempts to assign a value to the nearest object found in this environment hierarchy, and if none is found, assign it within the global environment. For example, below is a rudimentary adder.

f <- (function() { x <- 0; function(y) { x <<- x + y; x } })()
f(10) # 10
f(5)  # 15

The function f has an environment which has a parent environment which has x. Using <<-, we can access that x, whereas if we had <-, the result would have been y every time instead of keeping track of the sum. The reason for this is that <- would have created a copy of x in the local scope, and it would always be 0 since the value was copied from the parent environment.

For further information about these intricacies, you can also look at the relevant R documentation.

Robert Krzyzanowski
  • 9,294
  • 28
  • 24
  • 1
    Might be good to link to the documentation somewhere: https://stat.ethz.ch/R-manual/R-devel/library/base/html/assignOps.html – Thomas Mar 30 '14 at 18:12
  • If I am running a function, how can I say an object in the function is now the new value (both inside and outside the function), until it is assigned otherwise? – wolfsatthedoor Mar 30 '14 at 18:21
  • Well, `<-` assigns a local copy, and `<<-` assigns anything *but* a local copy, so if you really wish to have it set locally and in a parent scope, you could do `x <<- blah; x <- blah`. Why are you trying to do this? – Robert Krzyzanowski Mar 30 '14 at 18:23
  • See also http://stackoverflow.com/questions/5526322/examples-of-the-perils-of-globals-in-r-and-stata for why `<<-` is bad. – Ari B. Friedman Mar 30 '14 at 19:51
  • You know what? I am sick of this compulsive fear of `<<-`. The core R authors were perfectly aware of its dangers, and placed the operator in the language fully aware of its drawbacks. There are plenty of valid use cases if you are writing code that needs to "bubble up" references to parent environments. Sure, you could use `assign`, but it would simply be masking up the exact same code! Just because novices mistake `<<-` for global assignment does not mean it should be considered scary by even an intermediate R developer. – Robert Krzyzanowski Mar 30 '14 at 20:01
  • To be clear, I am suggesting using `<<-` for everything *but* global assignment where usage of that operator is appropriate. To do global assignment, I still agree that much clearer is `assign(name, x, envir = globalenv())`. – Robert Krzyzanowski Mar 30 '14 at 20:02
  • I need it because I am running an optimization of a function, and at the next iteration, I want to use a result of the last evaluation as an input for the next iteration. Does that make sense? I can elaborate. I have been having issues with this for hours. – wolfsatthedoor Mar 30 '14 at 20:57