Question: I don't understand why a globally-scoped list in R, cannot be modified from within a function as follows:

```
global <<- list()

f <- function(x) {
  global[[x]] <- "blah"
}

f(1)
f(2)
print(global)

```

The output is

```
list()

```

However, I was expecting the output:

```
[[1]]
[1] "blah"

[[2]]
[1] "blah"

```

Why is this? Do I have to declare each element of the list to be global as well?

It seems the same holds true for locally-scope variables declared outside of the function block.

Thanks!

Answer: 
Basically, since you are using the "<-" assignment, the function is creating a copy of the 'global' variable for use within the scope of the function. 

This can be seen by adding in a second function g() which alters the value of 'global' before it is printed out in f(), but this time using the "<<-" assignment. The first line in f() creates your locally scoped copy of 'global' for f(x), and then you update the global copy of 'global' using g(x).

```
global <<- list()

f <- function(x) {
  global[[x]] <- "blah"
  g(x)
  global
}

g <- function(x){
  global[[x]] <<- "newblah"
}

f(1)  #prints 'blah', despite the fact the g(x) has already updated the value

global  #prints 'newblah'

```

If f(x) were still referencing the global copy of 'global' it would print "newblah" which was assigned in g(x). Instead it prints the value which was assigned in f(x) to the locally scoped copy of 'global'.

However, printing 'global' outside any function shows that g(x) did in fact update the value for the global copy of 'global'.

Now, if you move g(x) inside f(x), then f(x) is now the parent of g(x). In this case, "<<-" assigns to the value of 'global' that is within the scope of f(x). So the global copy of 'global' is still empty, but if you print out 'global' in the scope of f() you get the updated value.

```
global <<- list()

f <- function(x) {
  global[[x]] <- "blah"

  g <- function(x){
    global[[x]] <<- "newblah"
  }

  g(x)
  global
}

f(1) #prints 'newblah'

global #empty

```
I believe you are simply a little confused by what `<<-` actually does. From `help("<<-")`:

> 
> The operators <<- and ->> are normally only used in functions, and
>  cause a search to 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.
> 
> 
> 

So it should be clear from this that `<<-` doesn't create a "global variable" wherever it is run. Indeed, that's not really how R works at all.

Rather, `<<-` performs a sort of "global assignment", allowing you to modify variables outside of the current scope.

It is a sort of crude version of the more precise `assign()`. The use of both of these tools is generally discouraged as they tend to break the functional programming paradigm that R uses (i.e. no side effects). Like anything else, they have their uses, but generally not for routine variable handling.