[R] Testing for existence inside a function

Gabor Grothendieck ggrothendieck at gmail.com
Tue May 15 19:43:21 CEST 2007


Right. Thanks. It should have been parent.frame().  Here is the correction:

chk3 <- function(x) {
       chr <- deparse(substitute(x))
       e <- parse(text = chr)
       structure(exists(chr, parent.frame()),
          is.name = length(e) == 1 && is.name(e[[1]]))
}
chk3(1) # structure(FALSE, is.name = FALSE)
ab <- 1
chk3(ab+1) # structure(FALSE, is.name = FALSE)
chk3(ab) # structure(TRUE, is.name = TRUE)
exists("x") # FALSE
chk3(x) # structure(FALSE, is.name = TRUE)
chk3(x+1) # structure(FALSE, is.name = FALSE)



On 5/15/07, Bert Gunter <gunter.berton at gene.com> wrote:
> I think parent.frame() is what is wanted, not parent.env(environment()) in
> your suggested solution:
>
> Consider this: (which does **not** however handle the arbitrary expressions
> as argument issue):
>
>
> foo1 <- function(z){
> cat(exists(deparse(substitute(z)),parent.frame()),
> exists(deparse(substitute(z)),parent.env(environment())),
> exists(deparse(substitute(z))),"\n")
> invisible()
>  }
>
> foo <-  function(x){
> y <- x
> foo1(y)
> }
>
> x<-1
>
> ## Then ...
> > foo(x)
> TRUE FALSE FALSE
>
> Note that parent.env() is the **enclosing environment** i.e. the environment
> in which foo1 is defined (lexical scoping); while parent.frame() is the
> frame of the caller of foo1, which is what is wanted if foo1 is to work when
> called within a function. Note that parent.frame() would also work when foo1
> is called at the command line.
>
> Further corrections/clarifications welcome, of course.
>
> Bert Gunter
> Genentech Nonclinical Statistics
>
>
>
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gabor Grothendieck
> Sent: Tuesday, May 15, 2007 10:06 AM
> To: Liaw, Andy
> Cc: r-help at stat.math.ethz.ch; Talbot Katz
> Subject: Re: [R] Testing for existence inside a function
>
> Maybe this:
>
>
> chk2 <- function(x) {
>        chr <- deparse(substitute(x))
>        e <- parse(text = chr)
>        structure(exists(chr, parent.env(environment())),
>           is.name = length(e) == 1 && is.name(e[[1]]))
> }
> chk2(1) # structure(FALSE, is.name = FALSE)
> ab <- 1
> chk2(ab+1) # structure(FALSE, is.name = FALSE)
> chk2(ab) # structure(TRUE, is.name = TRUE)
> exists("x") # FALSE
> chk2(x) # structure(FALSE, is.name = TRUE)
> chk2(x+1) # structure(FALSE, is.name = FALSE)
>
>
> On 5/15/07, Liaw, Andy <andy_liaw at merck.com> wrote:
> > Another thing to watch out for is that an argument to a function can be
> > an expression (or even literal constants), instead of just the name of
> > an object.  exists() wouldn't really do the right thing.  I'm not sure
> > how to properly do the exhaustive check.
> >
> > Andy
> >
> > From: Gabor Grothendieck
> > >
> > > Try this modification:
> > >
> > > > chk <- function(x) exists(deparse(substitute(x)),
> > > parent.env(environment()))
> > > > ab <- 1
> > > > chk(ab)
> > > [1] TRUE
> > > > exists("x")
> > > [1] FALSE
> > > > chk(x)
> > > [1] FALSE
> > >
> > >
> > >
> > > On 5/15/07, Talbot Katz <topkatz at msn.com> wrote:
> > > > Hi.
> > > >
> > > > Thanks once more for the swift response.  This solution
> > > works pretty well.
> > > > The only small glitch is if I pass the function an argument
> > > with the same
> > > > name as the function argument.  That is, suppose "x" is the
> > > argument name in
> > > > my user-defined function, and that object "x" does not
> > > exist.  If I call the
> > > > function f(x), i.e., using the non-existent object x as the
> > > argument value,
> > > > then the function says that x exists.
> > > >
> > > > Here is my example log:
> > > >
> > > > >chkex5 <- function(objn){
> > > > + c(exob=exists(deparse(substitute(objn))))
> > > > + }
> > > > >exists("objn")
> > > > [1] FALSE
> > > > >chkex5(objn)
> > > > exob
> > > > TRUE
> > > > >
> > > >
> > > > But I suppose I can live with this.  Thanks again!
> > > >
> > > >
> > > > --  TMK  --
> > > > 212-460-5430    home
> > > > 917-656-5351    cell
> > > >
> > > >
> > > >
> > > > >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
> > > > >Date: Tue, 15 May 2007 11:41:17 -0400
> > > > >
> > > > >Just need a bit more work:
> > > > >
> > > > >R> f <- function(x) exists(deparse(substitute(x)))
> > > > >R> f(y)
> > > > >[1] FALSE
> > > > >R> y <- 1
> > > > >R> f(y)
> > > > >[1] TRUE
> > > > >R> f(z)
> > > > >[1] FALSE
> > > > >
> > > > >Andy
> > > > >
> > > > >From: Talbot Katz
> > > > > >
> > > > > > 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.
> > > > > >
> > > > > > --  TMK  --
> > > > > > 212-460-5430        home
> > > > > > 917-656-5351        cell
> > > > > >
> > > > > >
> > > > > >
> > > > > > >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, contains
> > > > > > >information of Merck & Co., Inc. (One Merck Drive,
> > > > > > Whitehouse Station,
> > > > > > >New Jersey, USA 08889), and/or its affiliates (which
> > > may be known
> > > > > > >outside the United States as Merck Frosst, Merck Sharp
> > > & Dohme or MSD
> > > > > > >and in Japan, as Banyu - direct contact information
> > > for affiliates is
> > > > > > >available at
> > > http://www.merck.com/contact/contacts.html) that may be
> > > > > > >confidential, proprietary copyrighted and/or legally
> > > > > > privileged. It is
> > > > > > >intended solely for the use of the individual or
> > > entity named on this
> > > > > > >message. If you are not the intended recipient, and have
> > > > > > received this
> > > > > > >message in error, please notify us immediately by reply
> > > > > > e-mail and then
> > > > > > >delete it from your system.
> > > > > > >
> > > > > > >-------------------------------------------------------------
> > > > > > -----------------
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >-------------------------------------------------------------
> > > -----------------
> > > > >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.
> > > >
> > >
> > >
> > >
> >
> >
> >
> ----------------------------------------------------------------------------
> --
> > Notice:  This e-mail message, together with any attachment...{{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.
>
>



More information about the R-help mailing list