From ripley at stats.ox.ac.uk  Wed Jan  2 07:52:58 2013
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed, 02 Jan 2013 06:52:58 +0000
Subject: [Rd] Behavior or as.environment in function arguments/call (and
 force() behaviors...)
In-Reply-To: <50E36156.401@gmail.com>
References: <CABDUZc-98pUS=7cmSJodNKe7kqm5SxN0+ig63tgoe1iGwXcP1A@mail.gmail.com>
	<50E36156.401@gmail.com>
Message-ID: <50E3D94A.5000402@stats.ox.ac.uk>

On 01/01/2013 22:21, Duncan Murdoch wrote:
> On 13-01-01 4:35 PM, Jeff Ryan wrote:
>> Happy 2013!
>>
>> Can someone with more knowledge of edge case scoping/eval rules explain
>> what is happening below?  Happens in all the versions of R I have on
>> hand.
>
> Even though it is used as a default in a number of places, the pos==-1
> value is really poorly documented.  You need to look in the source, in

Hmm, I corrected that yesterday in R-patched and R-devel.

> particular src/main/envir.c, function pos2env.  There you'll see that
> pos==-1 is special cased to be the environment from which pos.to.env
> (or as.environment in your case) was called.  For non-negative values,
> it indexes the search list (i.e. the list returned by search().)  Other
> values are errors.

Actually for positive values: 0 is also an error.

>
> The trouble in your examples is that this location varies.  In Fn1, it
> is being called in the ls() call.  In Fn2, it is in the force() call. In
> Fn3 and Fn4, it's the Fn3/Fn4 call.
>
> In spite of what the docs say in ?get, I would rarely if ever use a pos
> argument to as.environment.  Use an environment and pass it as envir.
>
> Duncan Murdoch
>
>>
>> Behavior itself is confusing, but ?as.environment also provides no clue.
>>   The term used in that doc is 'search list', which is ambiguous, but the
>> see also section mentions search(), so I would *think* that is what is
>> intended.  Either way Fn1() below can't really be explained.
>>
>> Major question is what in the world is Fn1 doing, and why is Fn2 not
>> equal
>> to Fn3? [ Fn3/Fn4 are doing what I want. ]
>>
>>
>> Fn1 <- function(x="test",pos=-1,env=as.environment(pos)) {
>> ls(env)
>> }
>>
>> Fn2 <- function(x="test",pos=-1,env=as.environment(pos)) {
>> force(env)
>> ls(env)
>> }
>>
>> Fn3 <- function(x="test",pos=-1,env=as.environment(pos)) {
>> # should be the same as force() in Fn2, but not
>> # ?force
>> # Note:
>> #
>> #    This is semantic sugar: just evaluating the symbol will do the
>> #    same thing (see the examples).
>> env
>> ls(env)
>> }
>>
>> Fn4 <- function(x="test",pos=-1,env=as.environment(pos)) {
>> # same as Fn3
>> env <- env
>> ls(env)
>> }
>>
>> Fn1()
>> Fn2()
>> Fn3()
>> Fn4()
>> ls()
>>
>> ###################### output #########################
>>> Fn1()
>> [1] "doTryCatch" "expr"       "handler"    "name"       "parentenv"
>>
>>> Fn2()
>> [1] "env" "pos" "x"
>>
>>> Fn3()
>> [1] "Fn1" "Fn2" "Fn3" "Fn4"
>>
>>> Fn4()
>> [1] "Fn1" "Fn2" "Fn3" "Fn4"
>>
>> ### .GlobalEnv
>>> ls()
>> [1] "Fn1" "Fn2" "Fn3" "Fn4"
>>
>>> R.version
>>                 _
>> platform       x86_64-apple-darwin11.2.0
>> arch           x86_64
>> os             darwin11.2.0
>> system         x86_64, darwin11.2.0
>> status
>> major          2
>> minor          15.1
>> year           2012
>> month          06
>> day            22
>> svn rev        59600
>> language       R
>> version.string R version 2.15.1 (2012-06-22)
>> nickname       Roasted Marshmallows
>>
>>> R.version
>>                 _
>> platform       x86_64-apple-darwin11.2.0
>> arch           x86_64
>> os             darwin11.2.0
>> system         x86_64, darwin11.2.0
>> status         Under development (unstable)
>> major          3
>> minor          0.0
>> year           2012
>> month          12
>> day            28
>> svn rev        61464
>> language       R
>> version.string R Under development (unstable) (2012-12-28 r61464)
>> nickname       Unsuffered Consequences
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Behavior or as.environment in function arguments/call (and force() behaviors...)
Prof Brian Ripley