Skip to content
Prev 257669 / 398502 Next

What does the "<<-" operator mean?

On Fri, Apr 22, 2011 at 7:08 AM, Cliff Clive <cliffclive at gmail.com> wrote:
It's the 'superassignment' operator.  It does the assignment in the
enclosing environment. That is, starting with the enclosing frame, it
works its way up towards the global environment until it finds a
variable called ecov_xy, and then assigns to it.   If it never finds
an existing ecov_xy it creates one in the global environment.

The good use of superassignment is in conjuction with lexical scope,
where an environment stores state for a function or set of functions
that modify the state by using superassignment.

make.accumulator<-function(){
             a<-0
             function(x) {
                  a<<-a+x
                   a
            }
}
[1] 1
[1] 2
[1] 13
[1] 24


The Evil and Wrong use is to modify variables in the global environment.


      -thomas