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) Thanks much! Fang
I think you want something like exists("x", where = environment(), inherits = FALSE) -roger facS93 at hampshire.edu wrote:> 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) > > Thanks much! > > Fang > > ______________________________________________ > 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 >
<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