Hello, I am trying to test a function argument to see if it is or is not a useful number. However I cannot seem to find a test that works. For example> f = function(x) {+ print(exists("x")) + print(is.null(x)) + }>rm(x) > f(z)[1] TRUE Error in print(is.null(x)) : Object "z" not found exists gives TRUE, but then any other kind of test I try to run gives me an error. I need a test for existence that will work inside a function. Larry Howe
Larry Howe wrote:> Hello, > > I am trying to test a function argument to see if it is or is not a useful > number. However I cannot seem to find a test that works. For example > > >>f = function(x) { > > + print(exists("x")) > + print(is.null(x)) > + } > >>rm(x) >>f(z) > > [1] TRUE > Error in print(is.null(x)) : Object "z" not found > > exists gives TRUE, but then any other kind of test I try to run gives me an > error. I need a test for existence that will work inside a function. > > Larry Howe >This works because "x" exists in the function "f". Perhaps you want this instead? f <- function(x) { print(deparse(substitute(x))) print(exists(deparse(substitute(x)))) print(is.null(x)) invisible() } z <- 2 f(z) rm(z) f(z) Also, see the "where" argument in ?exists. HTH, --sundar
x exists within the function f so the first print is TRUE; however, exists does not try to evaluate it. is.null does try to evaluate it and so at that point it discovers the error. Perhaps you want this: f <- function(x) if (exists(deparse(substitute(x)))) print(is.null(x)) else print("none") if (exists("z")) rm(z) f(z) # "none" z <- 3 f(z) # FALSE z <- NULL f(z) # TRUE On 6/9/06, Larry Howe <linux at comjet.com> wrote:> Hello, > > I am trying to test a function argument to see if it is or is not a useful > number. However I cannot seem to find a test that works. For example > > > f = function(x) { > + print(exists("x")) > + print(is.null(x)) > + } > >rm(x) > > f(z) > [1] TRUE > Error in print(is.null(x)) : Object "z" not found > > exists gives TRUE, but then any other kind of test I try to run gives me an > error. I need a test for existence that will work inside a function. > > Larry Howe > > ______________________________________________ > 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 >
On Friday June 9 2006 16:06, Sundar Dorai-Raj wrote:> Larry Howe wrote:> > This works because "x" exists in the function "f". Perhaps you want this > instead? > > f <- function(x) { > print(deparse(substitute(x))) > print(exists(deparse(substitute(x)))) > print(is.null(x)) > invisible() > }Your example code works, but when I try to apply it to my function, it does not behave as expected. My variable seems to exist before I pass it into the function, but once inside the function it does not exist. I guess what I need is an in-depth guide to functions: scoping, arguments, parameters, etc. I am also becoming curious about how to pass values out of a function. Can someone point me in the right direction? Larry