# NOT RUN {
# Here is how to implement R's `<<-` operator, using `locate_`:
`%<<-%` <- function(lval, rval) {
lval_ <- arg(lval)
rval_ <- arg(rval)
target.env <- locate_(expr(lval_), parent.env(env(lval_)))
#note that `<-` is a primitive which requires its lvalue and call
#head to come from teh same env
env(lval_) <- target.env
do_(quo(`<-`, target.env), lval_, rval_)
}
x <- "not this one"
local({
x <- "this one"
local({
x <- "not this one either"
x %<<-% "this works like builtin <<-"
})
print(x)
})
# }
Run the code above in your browser using DataCamp Workspace