It is not a problem of do.call
, but substitute
which evaluate by default in the global environment.
So you need to tell it in which environment substitution must occur. Here obviously in the local envir of func3.
This should work:
do.call("curve",list(expr = substitute(my.func,
env = parent.frame())))
Edit thanks Dwin
As said in the comment substitute env Defaults to the current evaluation environment. So Why the code below works? The answer in the help of substitute
formal argument to a function or explicitly created using delayedAssign(), the expression slot of the promise replaces the symbol. If it is an ordinary variable, its value is substituted, unless env is .GlobalEnv in which case the symbol is left unchanged.
env = parent.frame(n=1)
is equivalent to .GlobalEnv
, that why the symbol (my.func) is left unchanged. So the correct answer would be :
do.call("curve",list(expr = substitute(my.func,
env = .GlobalEnv)))
To test , I open new R session :
func1 <- function (m, n) {
charac <- paste ("func2 <- function(x)", m, "*x^", n, sep = "")
eval(parse(text = charac))
return(func2)
}
func3 <- function (m, n) {
my.func <- func1 (m, n)
do.call("curve",list(expr = substitute(my.func,env = .GlobalEnv)))
}
Than I call
func3(2,6)