Manfredo
Manfredo

Reputation: 1740

Auto assignment

Recently I have been working with a set of R scripts that I inherited from a colleague. It is for me a trusted source but more than once I found in his code auto-assignments like

x <<- x

Is there any scope where such an operation could make sense?

Upvotes: 2

Views: 228

Answers (4)

Ben Bolker
Ben Bolker

Reputation: 226027

This is a mechanism for copying a value defined within a function into the global environment (or at least, somewhere within the stack of parent of environments): from ?"<<-"

The operators ‘<<-’ and ‘->>’ are normally only used in functions, and cause a search to be made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment.

I don't think it's particularly good practice (R is a mostly-functional language, and it's generally better to avoid function side effects), but it does do something. (@Roland points out in comments and @BrianO'Donnell in his answer [quoting Thomas Lumley] that using <<- is good practice if you're using it to modify a function closure, as in demo(scoping). In my experience it is more often misused to construct global variables than to work cleanly with function closures.)

Consider this example, starting in an empty/clean environment:

f <- function() {
     x <- 1     ## assignment
     x <<- x    ## global assignment
}

Before we call f():

x
## Error: object 'x' not found

Now call f() and try again:

f()
x
## [1] 1

Upvotes: 4

Tensibai
Tensibai

Reputation: 15784

For example:

x <- NA

test <- function(x) {
 x <<- x
}

> test(5)
> x
#[1] 5

That's a simple use here, <<- will do a parent environment search (case of nested functions declarations) and if not found assign in the global environment.

Usually this is a really bad ideaTM as you have no real control on where the variable will be assigned and you have chances it will overwrite a variable used for another purpose somewhere.

Upvotes: 1

Brian O&#39;Donnell
Brian O&#39;Donnell

Reputation: 1876

Alan gives a good answer: Use the superassignment operator <<- to write upstairs.

Hadley also gives a good answer: How do you use "<<-" (scoping assignment) in R?.

For details on the 'superassignment' operator see Scope.

Here is some critical information on the operator from the section on Assignment Operators in the R manual:

"The operators <<- and ->> are normally only used in functions, and cause a search to be made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment."

Thomas Lumley sums it up nicely: "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."

Upvotes: 1

USER_1
USER_1

Reputation: 2469

<<- 

is a global assignment operator and I would imagine there should hardly ever be a reason to use it because it effectively causes side effects.
The scope to use it would be in any case when one wants to define a global variable or a variable one level up from current environment.

Upvotes: 1

Related Questions