Question

I am facing a strange problem about do.call and curve:

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)))
}

func1 constructs func2 and func3 plots the constructed func2. But when I run func3, following error would be displayed:

> func3 (3, 6)
Error in curve(expr = function (x)  : 
  'expr' must be a function, or a call or an expression containing 'x'

However, while I run func1 and plot the output manually (without applying func3), func2 would be plotted:

my.func <- func1 (3, 6)
do.call("curve",list(expr = substitute(my.func)))

What happened here leads me to a confusion and I do not know why do.call can not plot func2 inside func3 local environment.

Thank you

Was it helpful?

Solution 2

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)

OTHER TIPS

You are making this overcomplicated - you don't need to do anything special when creating f2:

f1 <- function (m, n) {
  function(x) m * x ^ n
}
f3 <- function (m, n) {
  f2 <- f1(m, n)
  curve(f2)
}
f3(3, 6)

This could, of course, be made more concise by eliminating f1:

f4 <- function (m, n) {
  f2 <- function(x) m * x ^ n
  curve(f2)
}
f4(3, 6)

You can find more information about R's scoping rules (which makes this work) at https://github.com/hadley/devtools/wiki/Functions

This works:

func3 <- function (m, n) {
   my.func <- func1 (m, n); print(str(my.func))
   do.call(curve, list(expr=bquote( my.func) ) )
 }

You just need to remove line:

my.func <- func1 (m, n)

from func3.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top