[R] function parameters - testing

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Fri Jun 9 22:06:07 CEST 2006



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



More information about the R-help mailing list