I'm trying to use the curve3d
function in the emdbook
-package to create a contour plot of a function defined locally inside another function as shown in the following minimal example:
library(emdbook)
testcurve3d <- function(a) {
fn <- function(x,y) {
x*y*a
}
curve3d(fn(x,y))
}
Unexpectedly, this generates the error
> testcurve3d(2)
Error in fn(x, y) : could not find function "fn"
whereas the same idea works fine with the more basic curve
function of the base
-package:
testcurve <- function(a) {
fn <- function(x) {
x*a
}
curve(a*x)
}
testcurve(2)
The question is how curve3d
can be rewritten such that it behaves as expected.
fn <- function(x,y)
is replaced byfn <<- function(x,y)
, showing that the problem is the environment in whichcurve3d
is evaluating the expression. – Lichee