[R] function parameters - testing

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 9 22:10:35 CEST 2006


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
>



More information about the R-help mailing list