Re: [R] Removing objects and clearing memory

From: Denis Kazakiewicz <d.kazakiewicz_at_gmail.com>
Date: Wed, 13 Apr 2011 01:50:07 +0300

This should work also

data.frame(ls())
rm(list=ls()[-5]) #for instance

On 13.04.2011 01:45, Rolf Turner wrote:
> On 13/04/11 09:49, Denis Kazakiewicz wrote:
>> On 13.04.2011 00:39, Jim Silverton wrote:
>>> How do I remove all objects except one in R?
>>>
>> rm(list=ls()) #will remove ALL objects
>
> But he wanted to remove all objects ***except one***!!! So what's the
> point of this answer?
>
> I can't see any way except a rather round-about kludge to do what the
> OP wants.
>
> Create a function:
>
> rmabo <- function(x) {
> # Remove all but one.
> fff <- tempfile()
> save(list=deparse(substitute(x)),file=fff)
> rm(list=ls(pos=1),pos=1)
> load(fff,envir=.GlobalEnv)
> unlink(fff)
> }
>
> Save this function (otherwise it will get removed when you remove
> all objects except one, and rmabo is not the one, and will be lost):
>
> save(rmabo,file=".rmabo.rda")
>
> Whenever you want to remove all objects except one, attach the
> data base in which you have saved "rmabo". (You only need to do
> the attaching once per session; if you like you could put the attach
> command in your .Rprofile.)
>
> attach(".rmabo.rda")
>
> Then to remove all objects except "melvin" execute rmabo(melvin).
> E.g.:
>
> rm(list=ls)
> ls()
> character(0)
> melvin <- clyde <- irving <- fred <- 42
> ls()
> [1] "clyde" "fred" "irving" "melvin"
> rmabo(melvin)
> ls()
> [1] "melvin"
>
> This seems to work. Anyone have any cleverer ideas to accomplish the
> stated objective?
>
> cheers,
>
> Rolf Turner

        [[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. Received on Tue 12 Apr 2011 - 22:55:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 13 Apr 2011 - 00:20:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive