I'd like to make below program work.
I know, the error lies in list(f1, f2)
, but I didn't find a way to make it work so far. The problem is that f1
and f2
are not known outside func
's environment, but I'd like to just pass them as a name / symbol...
x = 1:2
func = function(f,x){
f1 = function(x)
sum(x)
f2 = function(x)
prod(x)
eval(substitute(f))(x)
}
func(f1, x) # works
func(f2, x) # works
sapply(list(f1,f2), function(f) func(f,x)) # cannot iterate over f's