1

Sorry this is a little complicated.

I want to capture an argument expression, but also know which environment it should be evaluated in. Something like this:

make.promise = function(x = print(b), b = 7) {
    expr = substitute(x)
    env  = parent.frame()

    function() {
        eval(expr, env)
    }
}

p1 = (
    function() {
        a = 2
        make.promise(print(a))
    }
)()

p2 = make.promise()

The problem is, if no argument is supplied for x, its environment becomes the local environment of make.promise(), and I don't know how to detect that. Is there a function other than substitute I could use that also captures the environment?

Owen
  • 38,836
  • 14
  • 95
  • 125
  • What are you trying to do? A little context would be helpful. Do you know about delayedAssign? – hadley Jun 26 '11 at 17:16
  • Actually, I solved my problem another way, now I'm just curious ;). And yes, I make extensive used of delayedAssign()... – Owen Jun 26 '11 at 20:33
  • care to share your alternative way of solving the problem as an Answer? – Gavin Simpson Jun 27 '11 at 14:57
  • Yes... I removed the default argument, so the environment would always be parent.frame() (I think this should be true). – Owen Jul 03 '11 at 01:37

1 Answers1

0

The simplest implementation of make.promise would be:

make.promise <- function(x) {
    function() x
}

But I don't think that's what you're looking for. I'm not aware of any way to find the environment associated - you might try email the r-devel mailing list.

hadley
  • 102,019
  • 32
  • 183
  • 245
  • Yeah... what I really wanted to do was get something that could be *re*evaluated more than once... but as I said up above if I don't have a default it should always be parent.frame(). But it doesn't seem (after more searching) there is any pure-R way to get the environment of a promise, though it could easily be done in C. – Owen Jul 03 '11 at 01:38