From murdoch.duncan at gmail.com  Fri May  9 13:45:25 2014
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Fri, 09 May 2014 07:45:25 -0400
Subject: [Rd] Access variable in attached but removed object
In-Reply-To: <m28uqbw7ch.fsf@krugs.de>
References: <m28uqbw7ch.fsf@krugs.de>
Message-ID: <536CBFD5.4070204@gmail.com>

On 09/05/2014, 6:54 AM, Rainer M Krug wrote:
>
> How can I access an object in an attached but deleted environment, when
> the object also exists in the .GolbalEnv?

Attaching a variable to the search list generally makes a copy of it, so 
it can't be "attached but deleted".  However, "making a copy" of an 
environment just copies the reference to it, so your environment still 
exists on the search list, it just doesn't have a name in the global 
environment.

>
> I hope the example below makes the question clear:
>
> --8<---------------cut here---------------start------------->8---
> tmp <- attach(what=NULL, name="org:variables")
> tmp$test = 13
> rm(tmp)
> test
> # > 13
> test <- 24
> test
> # > 24
> ls(all=TRUE)
> # > character(0)

I don't know why you would have seen character(0) here.  You should have 
seen "test" in the list, because you created it a couple of lines earlier.

> #
> # how can I access the variable test in the object org:variables in the
> # search path?
> #
> rm(test)
> test
> # > 13
> --8<---------------cut here---------------end--------------->8---
>
> Any suggestions?

You can use assign, or get a reference to the environment using 
as.environment("org:variables"), and access it within that.  For example,

assign("test", 24, pos="org:variables")

or

e <- as.environment("org:variables")
e$test <- 24

Duncan Murdoch



[Rd] Access variable in attached but removed object
Duncan Murdoch