8
What the operator means <<-
in R, what are your differences with respect to <-
and under what circumstances it may be useful?
8
What the operator means <<-
in R, what are your differences with respect to <-
and under what circumstances it may be useful?
12
I will explain with an example.
> objeto_fora_da_funcao <- 1
> f <- function() {
objeto_fora_da_funcao <- 10
return(objeto_fora_da_funcao)
}
Exits
> f()
# [1] 10
> objeto_fora_da_funcao
# [1] 1
Now, let’s move on <-
for <<-
within the function f
:
> objeto_fora_da_funcao <- 1
> f <- function() {
objeto_fora_da_funcao <<- 10
return(objeto_fora_da_funcao)
}
Notice the exits now:
Exits
> objeto_fora_da_funcao
# [1] 1
> f()
# [1] 10
> objeto_fora_da_funcao
# [1] 10
What’s behind this is the way R stores objects in "Environments", which could be seen as sets of objects (numbers, vectors, data.frames, functions, etc..).
<<-
is usually useful within functions, as the functions work with their own, temporary "Environments". Despite the functions of accessing global objects, the operator <-
is programmed to create (or reset) objects within the "Environment" of the respective function only. And that’s exactly why there is a <<-
. It will search for objects with that given name in all "Nvironments" from the most specific to the most comprehensive (known as "Global Nvironment").
This is useful when you want a function to change global variables.
6
Complementing Athos' response, here are some examples where the use of <<-
can be useful to change variables within the function environment itself without changing global variables:
Generating a counter that accumulates the value of i
in the function environment (based on Hadley’s book):
contador <- function() {
i <- 0
function() {
i <<- i + 1
i
}
}
cont1 <- contador()
cont1()
[1] 1
cont1()
[1] 2
cont1()
[1] 3
cont2 <- contador()
cont2() # note que o contador de cont2 é separdo de cont1
[1] 1
cont1()
[1] 4
Implementing memoization with local
and <<-
in the sequence of Fibonacci (adapted example from R Inferno):
fibonacci <- local({
memo <- c(0, 1, rep(NA, 100))
f <- function(x) {
if(x == 0) return(0)
if(x < 0) return(NA)
if(x > length(memo))
stop("’x’ too big for implementation")
if(!is.na(memo[x])) return(memo[x])
ans <- f(x-2) + f(x-1)
memo[x] <<- ans
ans
}
})
fibonacci(10)
[1] 34
Generating a Fibonacci sequence with replicate
and <<-
fibonacci <- function(n){
x0 <- 0
x1 <- 1
x <- numeric() # para assegurar que <<- não altere a variável global
fib <- function(){x <<- x1 + x0; x0 <<- x1; x1 <<- x}
c(x0, x1, replicate(n-2, fib()))
}
fibonacci(10)
[1] 0 1 1 2 3 5 8 13 21 34
Browser other questions tagged r
You are not signed in. Login or sign up in order to post.
Only that changing global variables from within a function goes against the good programming practices of most modern languages :P
– Lucas Soares
@It is that the main advantage is not necessarily to change the global variables, but rather the variables within the function environment itself, I will put an example.
– Carlos Cinelli
Athos, I would only change the last sentence to: "This is useful when you want a function to change variables of your parent environment." Hugs!
– Carlos Cinelli
@Athos, it remains a violation of good practice. To facilitate the understanding of the code, the location of bugs and a possible parallelization, the ideal is that each function changes only its environment.
– Lucas Soares