[R] control enviromnet

Gabor Grothendieck ggrothendieck at myway.com
Thu Oct 7 03:41:49 CEST 2004


 <facS93 <at> hampshire.edu> writes:

: 
: Hi all:
: 
: I would like to implement an option in my function so that it warns me of 
any 
: variables that are not defined in the current environment - if it needs to 
: look up variables in the parent frame, it tells me so. 
: 
: The following is an example and it does what I want, and I'd rather have the 
: environment control option inside this function instead of outside. Any help 
: would be greatly appreciated.
: 
: x = 1
: test = function(y) {
:  ans = y + x;
:  return(ans);
: }
: environment(test)=NULL
: test(y = 1)
: 

Here is one way.  

x <- 1
test <- function(y) {
	test <- function(y) x+y
	environment(test) <- NULL
	test(y)
}
test(9) # gives required error




More information about the R-help mailing list