[R] rm(list=ls())

R. Michael Weylandt michael.weylandt at gmail.com
Tue Mar 6 03:06:04 CET 2012


No, it won't give the same result. If you load packages or have hidden
variables / environments, they'll remain in the session or if you
change global options they'll stay. It's a somewhat frequently asked
question, but there's no way to get a "factory fresh" session without
restarting R. You can get pretty close though with

#####################################
####### rm(list = ls(all.names = TRUE)) # will get hidden (.xyz) variables
#####################################

and detach() applied to all of the package namespaces. You'll still
have some registered methods and whatnot around, but this will do most
of the cleanup.

Michael


On Mon, Mar 5, 2012 at 6:29 PM, Wet Bell Diver <wetbelldiver at gmail.com> wrote:
> R-2.14.2, Win7 x64
>
> Dear list,
>
> I have the habit of including rm(list=ls()) at the top of most of my
> scripts. However, I wonder whether rm(list=ls()) gives the exact same result
> as closing R and starting again fresh.
> In other words, is it possible that there will be differences on how a
> script runs under a fresh start of R versus when I "only" use rm(list=ls())
> , or should both approaches give the exact same starting situation every
> time?
>
> Thanks,
> Peter
>
> ______________________________________________
> 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