[Rd] Inspect a "delayed" assigned whose value throws an error?

Martin Maechler maechler at lynne.stat.math.ethz.ch
Tue Jan 27 15:56:19 CET 2015


>>>>> Henrik Bengtsson <hb at biostat.ucsf.edu>
>>>>>     on Mon, 26 Jan 2015 12:41:48 -0800 writes:

    > On Mon, Jan 26, 2015 at 12:24 PM, Hadley Wickham <h.wickham at gmail.com> wrote:
    >> If it was any other environment than the global, you could use substitute:
    >> 
    >> e <- new.env()
    >> delayedAssign("foo", stop("Hey!"), assign.env = e)
    >> substitute(foo, e)
    >> 
    >> delayedAssign("foo", stop("Hey!"))
    >> substitute(foo)

    > Hmm... interesting and odd.

    > Unfortunately, this doesn't seem to help for reaching into the
    > namespace of hgu133a.db and inspecting 'hgu133aPFAM', e.g.

    >> library("hgu133a.db")

    >> substitute(hgu133aPFAM, env=ns)
    > Error: hgu133aPFAM is defunct. Please use select() if you need access to PFAM
    > or PROSITE accessions.

    >> evalq(substitute(hgu133aPFAM), envir=ns)
    > Error: hgu133aPFAM is defunct. Please use select() if you need access to PFAM
    > or PROSITE accessions.

    >> evalq(substitute(hgu133aPFAM, env=ns), envir=ns)
    > Error: hgu133aPFAM is defunct. Please use select() if you need access to PFAM
    > or PROSITE accessions.

this *is* interesting..

Note that shortly after delayedAssign() was introduced into R,
we had

  R : Copyright 2005, The R Foundation for Statistical Computing
  Version 2.2.1  (2005-12-20 r36812)
  ISBN 3-900051-07-0

  ............

  > delayedAssign("x", pi^2) ; substitute(x)
  pi^2
  > 

so it also worked with the globalenv;  but that feature already
disappeared with 

  R : Copyright 2006, The R Foundation for Statistical Computing
  Version 2.3.0 (2006-04-24)

Almost surely as an inadvertent side effect of something else.

-----------------------------------

--> Quiz (even though it's not Friday) :

The help page for delayedAssign() contains a somewhat more
sophisticated example of 'promise's, and BTW an example that
worked also in *much* earlier version of R, probably in all
versions of R {I checked  R 1.0.1, probably the oldest still
running on my current Linux desktop} :

e <- (function(x, y = 1, z) environment())(1+2, "y", {cat(" HO! "); pi+2})

which gives an environment full of promises

Now, the following also works alread in R 1.0.1 :

> gete <- function(e)
   lapply(lapply(ls(env=e), as.name), 
         function(n) eval(substitute(substitute(X, e), list(X=n))))

> gete(e)
[[1]]
1 + 2

[[2]]
[1] "y"

[[3]]
{
    cat(" HO! ")
    pi + 2
}

> 

In newer versions of R, you can use  'ls(e)' instead of
'ls(env=e)', and we have bquote() to make it a tad shorter:

gete2 <- function(e)
   lapply(lapply(ls(e), as.name), function(n) eval(bquote(substitute(.(n), e))))

Apart from that (and from not using spaces and from silly things
such as 'L = lapply', and using L),
can anyone find a shorter (or "nicer") way to achieve the same
as gete(), or  gete2(), i.e., return a list with the promise expressions and
not evaluating e ?

You are not allowed to use 'x', 'y', and 'z' explicitly of
course, because here,
  gete0 <- function(e) list(substitute(x,e), substitute(y,e), substitute(z,e))
would be slightly shorter.

Martin



More information about the R-devel mailing list