Re: [Rd] Large discrepancies in the same object being saved to .RData

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Sun, 11 Jul 2010 17:25:45 -0400

On 11/07/2010 1:30 PM, Prof Brian Ripley wrote:
> On Sun, 11 Jul 2010, Tony Plate wrote:
>

>> Another way of seeing the environments referenced in an object is using 
>> str(), e.g.:
>>
>>> f1 <- function() {
>> + junk <- rnorm(10000000)
>> + x <- 1:3
>> + y <- rnorm(3)
>> + lm(y ~ x)
>> + }
>>> v1 <- f1()
>>> object.size(f1)
>> 1636 bytes
>>> grep("Environment", capture.output(str(v1)), value=TRUE)
>> [1] "  .. ..- attr(*, \".Environment\")=<environment: 0x01f11a30> "
>> [2] "  .. .. ..- attr(*, \".Environment\")=<environment: 0x01f11a30> "

>
> 'Some of the environments in a few cases': remember environments have
> environments (and so on), and that namespaces and packages are also
> environments. So we need to know about the environment of
> environment(v1$terms), which also gets saved (either as a reference or
> as an environment, depending on what it is).
>
> And this approach does not work for many of the commonest cases:
>
>> f <- function() {

> + x <- pi
> + g <- function() print(x)
> + return(g)
> + }
>> g <- f()
>> str(g)

> function ()
> - attr(*, "source")= chr "function() print(x)"
>> ls(environment(g))

> [1] "g" "x"
>
> In fact I think it works only for formulae.
>
>> -- Tony Plate
>>
>> On 7/10/2010 10:10 PM, Bill.Venables_at_csiro.au wrote:
>>> Well, I have answered one of my questions below.  The hidden
>>> environment is attached to the 'terms' component of v1.

>
> Well, not really hidden. A terms component is a formula (see
> ?terms.object), and a formula has an environment just as a closure
> does. In neither case does the print() method tell you about it --
> but ?formula does.
>

I've just changed the default print method for formulas to display the environment if it is not globalenv(), which is the rule used for closures as well. So now in R-devel:

 > as.formula("y ~ x")
y ~ x

as before, but

 > as.formula("y ~ x", env=new.env())
y ~ x
<environment: 01f83400>

Duncan Murdoch



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun 11 Jul 2010 - 21:28:13 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 12 Jul 2010 - 10:20:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive