The assignment function <<-will first search in the parent environment (the one that is the function), and up, if it is not found, it will create a new global environment.
, , f() , x . x :
x <- 0
wrapped_f = function(){
x<-2
function() x <<- 1
f()
print(paste("new x=", x))
}
wrapped_f()
#### "new x= 1"
print(x)
#### [1] 0
, x :
x <- 0
wrapped_f = function(){
# x<-2
f <- function() x <<- 1
f()
print(paste("new x=", x))
}
wrapped_f()
#### "new x= 1"
print(x)
#### [1] 1