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

From: Talbot Katz <topkatz_at_msn.com>
Date: Tue, 15 May 2007 11:30:31 -0400


Hi, Andy.

Thank you for the quick response! Unfortunately, none of these are exactly what I'm looking for. I'm looking for the following: Suppose object y exists and object z does not exist. If I pass y as the value of the argument to my function, I want to be able to verify, inside my function, the existence of y; similarly, if I pass z as the value of the argument, I want to be able to see, inside the function, that z doesn't exist.

The missing function just checks whether the argument is missing; in my case, the argument is not missing, but the object may not exist. And the way you use the exists function inside the user-defined function doesn't test the argument to the user-defined function, it's just hard-coded for the object y. So I'm sorry if I wasn't clear before, and I hope this is clear now. Perhaps what I'm attempting to do is unavailable because it's a bad programming paradigm. But even an explanation if that's the case would be appreciated.

>From: "Liaw, Andy" <andy_liaw_at_merck.com>
>To: "Talbot Katz" <topkatz_at_msn.com>,r-help_at_stat.math.ethz.ch
>Subject: RE: [R] Testing for existence inside a function [Broadcast]
>Date: Tue, 15 May 2007 11:03:12 -0400
>
>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}}



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. Received on Tue 15 May 2007 - 15:36:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 May 2007 - 16:31:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.