Hi all, Situation: there is a function `f' already defined by someone and provided in package. `f' looks like that: f <- function() { x+1 } i.e. `f' is not closed i.r.t. term `x' now I have my own function `g', where I'd like to override variable `x' while calling `f': x <- "dummy gloabal value" g <- function() { x <- 42 eval(f(), environment()) # how to make loacl `x' visible in `f'? } g() # => Error in x + 1 : non-numeric argument to binary operator Here comes the question: What is the right way to call `f' in order to override global value of `x' with the local value defined within of function `g' ? I see that i've missed something in docs for eval/environments and related. Thank you in advance. -- Valery.
Not an direct answer to your question, but why not do something like:> f <- function() x + 1 > x <- 10 > f()[1] 11> g <- function(x, ...) {+ my.f <- f + formals(my.f) <- c(list(x=NULL), formals(f)) + my.f(x, ...) + }> g(5)[1] 6 Andy> From: Khamenia, Valery > > Hi all, > > Situation: > > there is a function `f' already defined by someone and > provided in package. `f' looks like that: > > f <- function() { > x+1 > } > > i.e. `f' is not closed i.r.t. term `x' > > now I have my own function `g', where I'd like > to override variable `x' while calling `f': > > x <- "dummy gloabal value" > > g <- function() { > x <- 42 > eval(f(), environment()) # how to make loacl `x' visible in `f'? > } > > g() # => Error in x + 1 : non-numeric argument to binary operator > > Here comes the question: > > What is the right way to call `f' in order to override > global value of `x' with the local value defined within > of function `g' ? > > I see that i've missed something in docs for > eval/environments and related. > > Thank you in advance. > -- > Valery. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Gabor Grothendieck
2004-Oct-08 14:16 UTC
[R] provide extra variables to environment in call
Khamenia, Valery <V.Khamenia <at> biovision-discovery.de> writes: : : Hi all, : : Situation: : : there is a function `f' already defined by someone and : provided in package. `f' looks like that: : : f <- function() { : x+1 : } : : i.e. `f' is not closed i.r.t. term `x' : : now I have my own function `g', where I'd like : to override variable `x' while calling `f': : : x <- "dummy gloabal value" : : g <- function() { : x <- 42 : eval(f(), environment()) # how to make loacl `x' visible in `f'? : } : : g() # => Error in x + 1 : non-numeric argument to binary operator : : Here comes the question: : : What is the right way to call `f' in order to override : global value of `x' with the local value defined within : of function `g' ? : : I see that i've missed something in docs for eval/environments and related. f <- function() x+1 g <- function() { x <- 42 environment(f) <- environment() f() }
On Fri, 8 Oct 2004, Khamenia, Valery wrote:> Hi all, > > Situation: > > there is a function `f' already defined by someone and > provided in package. `f' looks like that: > > f <- function() { > x+1 > } > > i.e. `f' is not closed i.r.t. term `x' > > now I have my own function `g', where I'd like > to override variable `x' while calling `f': > > x <- "dummy gloabal value" > > g <- function() { > x <- 42 > eval(f(), environment()) # how to make loacl `x' visible in `f'? > } > > g() # => Error in x + 1 : non-numeric argument to binary operator > > Here comes the question: > > What is the right way to call `f' in order to override > global value of `x' with the local value defined within > of function `g' ? >This is messy because f() should not have been defined that way. You can't do it without modifying (a copy of) f(). If the package has a namespace, so that you can't override the definition of f() it may be impossible. If you can override the definition of f() you can do something like environment(f)<-environment() inside g(). On the other hand, you could then just replace the definition of f() with one that takes x as an argument. -thomas