[R] Testing for existence inside a function [Broadcast]

Liaw, Andy andy_liaw at merck.com
Tue May 15 17:03:12 CEST 2007


Not sure which one you want, but the following should cover it:

R> f <- function(x) c(x=missing(x), y=exists("y"))
R> f(1)
    x     y 
FALSE FALSE 
R> f()
    x     y 
 TRUE FALSE 
R> y <- 1
R> f()
   x    y 
TRUE TRUE 
R> f(1)
    x     y 
FALSE  TRUE 

Andy 

From: Talbot Katz
> 
> Hi.
> 
> I'm having trouble testing for existence of an object inside 
> a function.
> 
> Suppose I have a function:
> 
> f<-function(x){
> ...
> }
> 
> and I call it with argument y:
> 
> f(y)
> 
> I'd like to check inside the function whether argument y 
> exists.  Is this 
> possible, or do I have to either check outside the function 
> or pass the name 
> of the argument as a separate argument?
> 
> If I do exists(x)  or exists(eval(x)) inside the function and 
> y does not 
> exist, it generates an error message.  If I do exists("x") it 
> says that x 
> exists even if y does not.  If I had a separate argument to 
> hold the text 
> string "y" then I could check that.  But is it possible to check the 
> existence of the argument inside the function without passing 
> its name as a 
> separate argument?
> 
> Thanks!
> 
> --  TMK  --
> 212-460-5430	home
> 917-656-5351	cell
> 
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}



More information about the R-help mailing list