[Rd] Access variable in attached but removed object

Rainer M Krug Rainer at krugs.de
Fri May 9 14:07:38 CEST 2014


Duncan Murdoch <murdoch.duncan at gmail.com> writes:

> 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 was aware that my wording was not the best - your explanation makes
the whole process much clearer - thanks.

>
>>
>> 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.

You are right. Wrong copy - paste. My fault.

>
>> #
>> # 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

Perfect - that is what I was looking for.
Both will work perfectly in my case.

Thanks  lot,

Rainer

>
> Duncan Murdoch

-- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer at krugs.de

Skype:      RMkrug

PGP: 0x0F52F982
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 494 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20140509/3a122185/attachment.bin>


More information about the R-devel mailing list