Why doesn’t the following work? Ie, why doesn’t calling “$<-” on an environment have a visible effect outside of the function?
myAssign <- function(env, name, value) {
"$<-"(env, name, value)
}
e <- new.env()
myAssign(e, "x", 1)
e$x # NULL
And also
myAssign(e, "x", 1)$x # NULL
Whereas, if we do this at the top level:
"$<-"(e, "x", 1)
e$x # 1
Thanks!
It does have an effect, just not the one you’re looking for!
The reason is that
$<-evaluates its second argument in a non-standard way (as it must, to getxinstead ofeval(x)ine$x <- 1, if that makes any sense. Tryenv[[name]] <- value