From hadley at rice.edu  Mon May  2 16:34:28 2011
From: hadley at rice.edu (Hadley Wickham)
Date: Mon, 2 May 2011 09:34:28 -0500
Subject: [Rd] Using substitute to access the expression related to a
	promise
In-Reply-To: <4DBEBB52.7050508@gmail.com>
References: <BANLkTi=PARrZvyvDJsH4x8GDCY7kLHm+=w@mail.gmail.com>
	<4DBEBB52.7050508@gmail.com>
Message-ID: <BANLkTimcELni6NJAoKYOj5WbqC7rx5VmtQ@mail.gmail.com>

On Mon, May 2, 2011 at 9:10 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
> On 02/05/2011 9:53 AM, Hadley Wickham wrote:
>>
>> Hi all,
>>
>> The help for delayedAssign suggests that you can use substitute to
>> access the expression associated with a promise, and the help for
>> substitute says: "If it is a promise object, i.e., a formal argument
>> to a function or explicitly created using ?delayedAssign()?, the
>> expression slot of the promise replaces the symbol.
>>
>> But this doesn't seem to work:
>>
>> > ?a<- 1
>> > ?b<- 2
>> > ?delayedAssign("x", {message("assigning..."); a + b})
>> > ?substitute(x)
>> x
>> > ?x
>> [1] 3
>>
>> Is this a bug in substitute?
>>
>
> I think it is a design flaw rather than a bug: ?the global environment is
> handled specially. ?If you put those lines into a function you'll see
> different behaviour. ?I think if you really carefully read the documentation
> you'll find it says this.

I guess you mean this sentence: "If it is an ordinary variable, its
value is substituted, unless ?env? is ?.GlobalEnv? in which case the
symbol is left unchanged."  But I think the conditioning is confusing,
because it implies that this condition only comes into play if x is an
ordinary variable, which is not the case in my example.

Hadley



-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



[Rd] Using substitute to access the expression related to a promise
Hadley Wickham