Reputation: 8523
Guys, this drives me crazy.
This works as expected:
eobj <- substitute(obj <- list(a, b), list(a = 32, b = 33))
eval(eobj)
obj
[[1]]
[1] 32
[[2]]
[1] 33
Now, try this:
efun <- substitute(fun <- function() a+ b, list(a = 32, b = 33))
str(efun)
# language fun <- function() 32 + 33
eval(efun)
fun
# function() a+ b
What is going on here? How on earth eval
gets it hands on the original form of the expression?
Upvotes: 6
Views: 825
Reputation: 50704
Cause when you print fun
it's actually print source of function (see attributes(fun)
) which isn't modify by substitute
.
Notice that when you define a
or b
in global workspace function result are the same.
You can see actual code of function by body(fun)
.
Or compare:
print.function(fun, useSource=FALSE)
# function ()
# 32 + 33
print.function(fun, useSource=TRUE) # Which is default
# function() a+ b
Upvotes: 8