Suppose I have a closure add_y(y)
which returns a function that adds y
to its input.
add_y <- function(y) {
function(x) {
x + y
}
}
add_4 <- add_y(4)
So the value of add_4
is a function that adds 4 to its input. This works. I would like to be use dput
to show the definition of add_4
as
function(x) {
x + 4
}
but this is not what dput returns.
add_y <- function(y) {
function(x) {
x + y
}
}
add_4 <- add_y(4)
dput(add_4)
#> function (x)
#> {
#> x + y
#> }
Is there a way to obtain source code that would run outside of the enclosing environment?
formals
andargs
within thelocal
environment? – Hyperpituitarismdput
gives you the function but not the environment.y
is stored in the environment. If you want good advice, you should explain why you need this. Whydput
instead of serializing the closure? – Marcus