Hello,
I want to call a function fx given by name, where some global
variables (in the environment of fx) are passed to the function. For
compatibility reasons I cannot modify the parameter list of fx and I
want to avoid setting variables in the global environment (e.g. via -)
Is there a way,
Try this:
x - 7
fx - function(y) print(x*y)
f - function(fun, x) {
myfx - eval(parse(text=deparse(fun)))
myfx(3)
}
f(fx,2) # uses 2, not 7, for x
This creates a new function myfx which has the same functionality
as fx but is created in the body of f and therefore has that
The reason in your example that fx() doesn't find 'x' is that the lexical
scope of fx() does not include 'x'. So, this is what must be fixed, in one
way or another.
One simple way to make your example work is to define fx() in a place where
its lexical scope includes the variables you want it
Seems to me what you want is dynamic scoping: `x' is not defined in `fx'.
You want `x' to be found in the scope of the function(s) that calls `fx',
rather than the environment where `fx' is defined. I was told (thanks,
Robert!) that that is a very bad idea: as the author of `fx', you want some
PROTECTED],R-Help [EMAIL PROTECTED]
Subject: Re: [R] do.call and environments
The reason in your example that fx() doesn't find 'x' is that the lexical
scope of fx() does not include 'x'. So, this is what must be fixed, in one
way or another.
One simple way to make your example work