Clearing all user-defined objects in R workspace
Asked Answered
P

2

21

I'm working with Rserve via Ruby bindings. It's pretty trivial to establish a connection to Rserve, and I assume its a good idea to persist that connection globally to avoid the overhead of tearing it down and re-building it as needed (I'm not operating in a multi-threaded environment).

Since the objects defined will stick around, and potentially class with later operations, I want to clear them out. I've seen:

myvar = 1
rm(myvar)

However, I would rather re-initialize everything, to avoid having to manually keep track of whats defined. Is this possible? Is there a significant overhead associated with it if so?

Passim answered 26/8, 2013 at 20:51 Comment(5)
Are you looking for rm(list=ls())?Fina
From ?rm : ## remove (almost) everything in the working environment. ## You will get no warning, so don't do this unless you are really sure. rm(list = ls())Fina
I think adding rm(list=ls(all=TRUE)) will remove everything or more at least.Hanoverian
As far as clashes go, this question may be relevant: #2823032Vmail
Yes, rm(list=ls()) looks like it will hopefully do the trick. Thanks allPassim
Q
43

it is a bit dangerous but: rm(list=ls()) really, don't do this.

Quotient answered 26/8, 2013 at 20:53 Comment(4)
how to remove some variables?Iain
@Iain This question may help: #2823032Quotient
Why is it a bit dangerous? Is it just that you might accidentally delete something you didn't want to?Tokenism
@user1205901 That is right. My personal rule is never write it in a script, but it is OK to use at the interactive command line.Quotient
B
1

If you are working with a dataset let say named data_new, you can use the following comment to remove all information about data_new from your workspace:

rm(data = data_new)

Bacchanal answered 30/5, 2017 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.