[R] Removing named objects using rm(..)
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Dec 11 13:09:58 CET 2012
On 12-12-11 12:42 AM, Worik R wrote:
>>
>> You may find it more reliable to define an environment in which you
>> will be storing your data (perhaps globalenv(), perhaps something created
>> by new.env()) and then testing for existence of a dataset by a given name
>> in that environment.
>>
>>
> I did that.
>
> PAIR.ENV <- new.env()
> ....
> get("USDCHF", env=PAIR.ENV)
>
> returns trhe USDCHF defined in timeSeries
>
> This is very hard!
I think if you had followed my advice (reading up on scoping) you
wouldn't find it so hard. new.env() creates a new environment whose
parent (or enclosure) is globalenv(). So when you do a search in
PAIR.ENV, you'll search there first, then in globalenv(), then in its
parent, etc.
If you don't want this to happen, don't set globalenv() as the parent.
emptyenv() is likely what you want instead:
PAIR.ENV <- new.env(parent=emptyenv())
means that only the objects you put there will be found.
Duncan Murdoch
>
> Worik
>
> Bill Dunlap
>> Spotfire, TIBCO Software
>> wdunlap tibco.com
>>
>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On Behalf
>>> Of Worik R
>>> Sent: Monday, December 10, 2012 5:47 PM
>>> To: Duncan Murdoch
>>> Cc: r-help
>>> Subject: Re: [R] Removing named objects using rm(..)
>>>
>>> On Tue, Dec 11, 2012 at 2:27 PM, Duncan Murdoch
>>> <murdoch.duncan at gmail.com>wrote:
>>>
>>>> On 12-12-10 7:33 PM, Worik R wrote:
>>>>
>>>>> Let me restate my question.
>>>>>
>>>>> Is there a straightforward way of ensuring I can use the variable name
>>>>> USDCHF?
>>>>>
>>>>
>>>> You can use any legal variable name. The only risk is that you will
>>>> overwrite some other variable that you created. You can't overwrite
>>>> variables from packages. (You might mask them, but they are still
>>>> accessible using the :: notation. E.g. after you set
>>>>
>>>> USDCHF <- NULL
>>>>
>>>
>>> Exactly. I got around this by assigning NULL to the variable names that
>> I
>>> would have deleted. Then instead of testing for existence I tested for
>>> NULL.
>>>
>>>
>>>>
>>>> you can still access the one in timeSeries using
>>>>
>>>> timeSeries::USDCHF
>>>
>>>
>>> Christ. That is what I wanted to delete. I read the scoping section of
>>> R-Lang (again) and nothing I could see prepared me for the shock of...
>>>
>>>> library(timeSeries)
>>>> nrow(USDCHF)
>>> [1] 62496
>>>> rm(USDCHF)
>>> Warning message:
>>> In rm(USDCHF) : object 'USDCHF' not found
>>>> nrow(USDCHF)
>>> [1] 62496
>>>
>>>
>>> The message from rm was that USDCHF did not exist. But I can still
>> access
>>> its properties with nrow.
>>>
>>> This is very broken. I would not have believed I would see that in the
>>> 21st century with a modern language. (Oh wait, there is Javascript and
>>> PHP, so in comparison R is not that broken)
>>>
>>> I am not new to R, I have been (mis)using it for 5 years. I love aspects
>>> of R, but this and a few other things (lack of debugging support and
>>> ignoring the "principle of least surprise" are two biggies) are very
>>> frustrating. Without debugging support or more help from the compiler
>>> (like a "cannot rm EURCHF" message instead of a lie) R causes as many
>>> problems as it solves.
>>>
>>> Sigh. Thanks for the help.
>>>
>>> Worik
>>>
>>>
>>>
>>>
>>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list