I would like to ask how function h can see variable v1 defined in function g. Both h and g are defined in f. The solution I thought is to define the environment e1 and to put there v1.
But this is not working, I get character(0) when I run ls(e1), and I do not know why.
e1 <- new.env()
f <- function(){
g <- function(){
e1$v1 <- 5
}
h <- function(){
print(e1$v1)
}
h()
}
f()
Thank you in advance
You never actually run the function
g(). Try addingg()just beforeh().(Although, I feel compelled to add, for the benefit of folks finding this question in the future, that this sort thing, which works agains the natural scoping behavior of R is a dangerous things to play around with. So be careful!)