[Rd] Large discrepancies in the same object being saved to .RData
Duncan Murdoch
murdoch.duncan at gmail.com
Sun Jul 11 23:25:45 CEST 2010
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
More information about the R-devel
mailing list