# Here is how to implement R's `<<-` operator, using `locate_`:
`%<<-%` <- function(lval, rval) {
lval_ <- arg(lval)
name <- expr(lval_)
target.env <- locate_(name, parent.env(env(lval_)))
assign(as.character(name), rval, envir=target.env)
}
x <- "not this one"
local({
x <- "this one"
local({
x <- "not this one either"
x %<<-% "this works like builtin <<-"
})
print(x)
})
print(x)
Run the code above in your browser using DataLab