If have a function that returns a function:
makeFunction <- function (d) {
function (x) { x/d }
}
f <- makeFunction(2)
print(f)
# function (x) { x/d }
# <environment: 0x34216b8>
f(4) # 2, as expected
What I want is to have f
simply "be" function (x) { x/2 }
. By "be", I mean that it doesn't carry that environment around (with the value of d
in it), and when I print
it, it shows function (x) { x/2 }
.
Is such a thing possible?
Attempts so far
I can get almost there with substitute
:
makeFunction.sub <- function (d) {
substitute(function (x) { x/d }, list(d=d))
}
g <- makeFunction.sub(2)
print(g)
# function(x) {
# x/2
# }
However, g
is not a function - it's a call. So to get the function I need to eval
it. But then it loses the 2 and reverts to d
:
eval(g)
# function (x) { x/d }
Is there any way I can make the returned function forget about d
and just substitute 2 directly?
(In practice, I don't mind carrying around the environment, but I was just curious...)
Solution
Going to mark as dupe (well, subset) of this question/answer (thanks @joran).
That question & answer are much more detailed, so for the record:
makeFunction <- function (d) {
of <- function (x) {}
# do the substitution of d in the body
body(of) <- substitute(x/d, list(d=d))
# don't need to carry around the little local environment anymore
environment(of) <- .GlobalEnv
return(of)
}
h <- makeFunction(2)
print(h)
# function (x)
# x/2