Why is there no within.environment function?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Why is there no within.environment function?

Richard Cotton-2
If I want to assign some variables into an environment, it seems
natural to do something like

e <- new.env()
within(e,
    {
      x <- 1:5
      y <- runif(5)
    }
)

This throws an error, since within.environment doesn't exist.  I
realise I can work around it using

as.environment(within(as.list(e),
    {
      x <- 1:5
      y <- runif(5)
    }
))

Just wondering why I can't use within directly with environments.

--
4dpiecharts.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no within.environment function?

Gabor Grothendieck
On Wed, Mar 21, 2012 at 5:51 PM, Richard Cotton <[hidden email]> wrote:

> If I want to assign some variables into an environment, it seems
> natural to do something like
>
> e <- new.env()
> within(e,
>    {
>      x <- 1:5
>      y <- runif(5)
>    }
> )
>
> This throws an error, since within.environment doesn't exist.  I
> realise I can work around it using
>

'with' already does that:

e <- new.env()
with(e, {
   x <- 1.5
   y <- runif(5)
})
ls(e) # lists x and y

Also since proto objects are environments this works:

library(proto)
p <- proto(x = 1.5, y = runif(5))
p$ls() # lists x and y

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no within.environment function?

William Dunlap
In reply to this post by Richard Cotton-2
Wouldn't within.environment be identical to with.environment?
  > e <- new.env()
  > with(e, { One <- 1 ; Two <- 2+2i ; Theee <- One + Two })
  > objects(e)
  [1] "One"   "Theee" "Two"
It might make the transition between lists and environments
simpler if within.environment  existed.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Richard Cotton
> Sent: Wednesday, March 21, 2012 2:51 PM
> To: [hidden email]
> Subject: [Rd] Why is there no within.environment function?
>
> If I want to assign some variables into an environment, it seems
> natural to do something like
>
> e <- new.env()
> within(e,
>     {
>       x <- 1:5
>       y <- runif(5)
>     }
> )
>
> This throws an error, since within.environment doesn't exist.  I
> realise I can work around it using
>
> as.environment(within(as.list(e),
>     {
>       x <- 1:5
>       y <- runif(5)
>     }
> ))
>
> Just wondering why I can't use within directly with environments.
>
> --
> 4dpiecharts.com
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no within.environment function?

Peter Dalgaard-2

On Mar 21, 2012, at 23:01 , William Dunlap wrote:

> Wouldn't within.environment be identical to with.environment?
>> e <- new.env()
>> with(e, { One <- 1 ; Two <- 2+2i ; Theee <- One + Two })
>> objects(e)
>  [1] "One"   "Theee" "Two"
> It might make the transition between lists and environments
> simpler if within.environment  existed.
>

evalq() would be rather more to the point. Then again, with() _is_ really just a sugar-coated evalq().

within() was quite specifically created because you couldn't do the same kind of things with data frames that you could do with environments, so the current thread does seem a bit peculiar to me... (The original design of within() would modify the object in-place, like fix(), but Luke objected.)


 


> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:[hidden email]] On Behalf
>> Of Richard Cotton
>> Sent: Wednesday, March 21, 2012 2:51 PM
>> To: [hidden email]
>> Subject: [Rd] Why is there no within.environment function?
>>
>> If I want to assign some variables into an environment, it seems
>> natural to do something like
>>
>> e <- new.env()
>> within(e,
>>    {
>>      x <- 1:5
>>      y <- runif(5)
>>    }
>> )
>>
>> This throws an error, since within.environment doesn't exist.  I
>> realise I can work around it using
>>
>> as.environment(within(as.list(e),
>>    {
>>      x <- 1:5
>>      y <- runif(5)
>>    }
>> ))
>>
>> Just wondering why I can't use within directly with environments.
>>
>> --
>> 4dpiecharts.com
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no within.environment function?

Gavin Simpson
In reply to this post by William Dunlap
On Wed, 2012-03-21 at 22:01 +0000, William Dunlap wrote:

> Wouldn't within.environment be identical to with.environment?
>   > e <- new.env()
>   > with(e, { One <- 1 ; Two <- 2+2i ; Theee <- One + Two })
>   > objects(e)
>   [1] "One"   "Theee" "Two"
> It might make the transition between lists and environments
> simpler if within.environment  existed.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com

One doesn't normally think of `with()` as changing it's `data` argument,
which might be one reason the connection to `with.environment()` was not
made here.

> d <- data.frame()
> with(d, {
+ A <- 1:3
+ B <- 1:3
+ })
> d
data frame with 0 columns and 0 rows

The behaviour of `with.environment()` makes sense once you think about
it as there is only one environment `e` (from your example), and when it
is updated during the call to `with()` it isn't a copy that is being
updated but the real thing. So I can see why it was overlooked.

G

>
> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]] On Behalf
> > Of Richard Cotton
> > Sent: Wednesday, March 21, 2012 2:51 PM
> > To: [hidden email]
> > Subject: [Rd] Why is there no within.environment function?
> >
> > If I want to assign some variables into an environment, it seems
> > natural to do something like
> >
> > e <- new.env()
> > within(e,
> >     {
> >       x <- 1:5
> >       y <- runif(5)
> >     }
> > )
> >
> > This throws an error, since within.environment doesn't exist.  I
> > realise I can work around it using
> >
> > as.environment(within(as.list(e),
> >     {
> >       x <- 1:5
> >       y <- runif(5)
> >     }
> > ))
> >
> > Just wondering why I can't use within directly with environments.
> >
> > --
> > 4dpiecharts.com
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel