From murdoch.duncan at gmail.com  Sun Jul 11 23:25:45 2010
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Sun, 11 Jul 2010 17:25:45 -0400
Subject: [Rd] Large discrepancies in the same object being saved to
	.RData
In-Reply-To: <alpine.LFD.2.00.1007111715220.20108@gannet.stats.ox.ac.uk>
References: <62C82B39B8A85E4B95A18F7F7B852F87057236625D@exvic-mbx03.nexus.csiro.au>	<4C346F1A.8060302@gmail.com>	<AANLkTimUSBF4mQpc7ZC0kovoBTUiSgwPIiM_acr9ds2H@mail.gmail.com>	<4C391204.3040601@gmail.com>	<1BDAE2969943D540934EE8B4EF68F95FB26EB6E6D3@EXNSW-MBX03.nexus.csiro.au>	<1BDAE2969943D540934EE8B4EF68F95FB26EB66350@EXNSW-MBX03.nexus.csiro.au>	<4C39DE7C.8060802@acm.org>
	<alpine.LFD.2.00.1007111715220.20108@gannet.stats.ox.ac.uk>
Message-ID: <4C3A36D9.3010400@gmail.com>

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



[Rd] Large discrepancies in the same object being saved to .RData
Duncan Murdoch