4

Often the parent environment is the global environment.

But occasionally it isn't. For example in functions within functions, or in an error function in tryCatch().

Strictly speaking, does <<- assign to the global environment, or simply to the parent environment?

smci
  • 32,567
  • 20
  • 113
  • 146
stevec
  • 41,291
  • 27
  • 223
  • 311
  • 3
    The documentation says, "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.", which seems to answer your question fairly directly. – joran Feb 22 '19 at 15:50
  • I illustrate this in one of my other answers: https://stackoverflow.com/questions/10904124/global-and-local-variables-in-r/10904810#10904810 Hopefully it helps – Dason Feb 22 '19 at 15:52
  • @joran: No that's not so clear. The search is from bottom-up. That's implied but not stated (what if there were multiple hits in different environments?). The doc could be made explicit. – smci Mar 27 '19 at 07:32

2 Answers2

8

Try it out:

env = new.env()
env2 = new.env(parent = env)

local(x <<- 42, env2)
ls(env)
# character(0)
ls()
# [1] "env"  "env2" "x"

But:

env$x = 1
local(x <<- 2, env2)
env$x
# [1] 2

… so <<- does walk up the entire chain of parent environments until it finds an existing object of the given name, and replaces that. However, if it doesn’t find any such object, it creates a new object in .GlobalEnv.

(The documentation states much the same. But in a case such as this nothing beats experimenting to gain a better understanding.)

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    It is not accurate to say it continues until it hits the `.GlobalEnv`, it goes all the way up, and then if it's not found, creates it in the global environment. Try `attach(iris);Species <<- "hello";ls()` or `mean <<- "bar"` – moodymudskipper Feb 22 '19 at 16:50
  • 1
    @Moody_Mudskipper Right, I fixed the wording. – Konrad Rudolph Feb 22 '19 at 17:21
5

Per the documentation:

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.

Use of this operator will cause R to search through the environment tree until it finds a match. The search starts at the environment in which the operator is used and moves up the stack from there. So it's not guaranteed to be a "global" assignment, but could be.

As sindri_baldur points out, if the variable is not found in any existing environment, a new one will be created at the global level.

Lastly, I should point out that use of the operator is confusing more often than it is helpful, as it breaks the otherwise highly functional nature of R programming. There's more than likely a way to avoid using <<-.

jdobres
  • 11,339
  • 1
  • 17
  • 37