[R] provide extra variables to environment in call

Gabor Grothendieck ggrothendieck at myway.com
Fri Oct 8 16:16:07 CEST 2004


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




More information about the R-help mailing list