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

2011-05-02 Thread Hadley Wickham
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?

 sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
...

Hadley

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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Using substitute to access the expression related to a promise

2011-05-02 Thread Gabor Grothendieck
On Mon, May 2, 2011 at 9:53 AM, Hadley Wickham had...@rice.edu 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?

 sessionInfo()
 R version 2.13.0 (2011-04-13)
 Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

It works differently if the assign environment is the global
environment or not.  This is actually mentioned on the help page for
substitute though its precise meaning may not be completely clear from
the wording.

Try this:

 e - new.env()
 delayedAssign(x, {message(assigning...); a + b},, e)
 substitute(x, e)
{
message(assigning...)
a + b
}



-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Using substitute to access the expression related to a promise

2011-05-02 Thread Duncan Murdoch

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 suggested regularizing this several years ago, but there were worries 
that it would break some common usage.


Duncan Murdoch


  sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
...

Hadley



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Using substitute to access the expression related to a promise

2011-05-02 Thread Gabor Grothendieck
On Mon, May 2, 2011 at 10:10 AM, Duncan Murdoch
murdoch.dun...@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 suggested regularizing this several years ago, but there were worries that
 it would break some common usage.

 Duncan Murdoch

Perhaps an argument could be added to indicate whether the global
environment was treated specially or not.  If it defaulted to the
current behavior it would still be backwardly compatible.

The other limitation that I have come across here is the asymmetry of
being able to extract the data of the promise but not the environment.
 Being able to copy a promise without evaluating it is another
operation that would be nice.   Currently one can only do these two
things at the C level.

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Using substitute to access the expression related to a promise

2011-05-02 Thread Hadley Wickham
On Mon, May 2, 2011 at 9:10 AM, Duncan Murdoch murdoch.dun...@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/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel