r - pass objects to nested functions using environments -
i want pass objects 1 function nested function assigning environments. below sample of code not work. how can happen assigning environments in function?
sumi <- function(x,y) { my.env <- new.env() my.env$rumi <- function() { my.env$k <- x[1] my.env$f <- y[1] } k <- get("k", my.env) f <- get("f", my.env) z <- k+f return(z) }
the code defines never runs rumi
objects have created if run never are.
adding line marked ### works:
sumi <- function(x,y) { my.env <- new.env() my.env$rumi <- function() { my.env$k <- x[1] my.env$f <- y[1] } my.env$rumi() ### k <- get("k", my.env) f <- get("f", my.env) z <- k+f return(z) } sumi(1, 2) ## [1] 3
Comments
Post a Comment