Clear R Environment of All Objetcs & Packages

How do I clear only a few specific objects from the workspace?

You'll find the answer by typing ?rm

rm(data_1, data_2, data_3)

How can I remove all objects but one from the workspace in R?

Here is a simple construct that will do it, by using setdiff:

rm(list=setdiff(ls(), "x"))

And a full example. Run this at your own risk - it will remove all variables except x:

x <- 1
y <- 2
z <- 3
ls()
[1] "x" "y" "z"

rm(list=setdiff(ls(), "x"))

ls()
[1] "x"

Clearing all user-defined objects in R workspace

it is a bit dangerous but:

rm(list=ls())

really, don't do this.

Remove all R Data objects from Global Environment

The data tab seems to hold anything with more than one dimension.

If you do ls(), you get character strings of the names of all the objects in the global environment. You can represent any of these objects by calling get("object_name"), so you can get the number of dimensions it has by calling length(dim(get("object_name"))). If this value is greater than 1, you know this is one of the objects you want to remove.

Therefore, all you need to do is apply length(dim(get("object_name"))) > 1 to the names of the global objects, as obtained by ls(). You can do this with sapply:

rm(list = ls()[sapply(ls(), function(x) length(dim(get(x))) > 1)])

rm(list=ls()) doesn't completely clear the workspace

attach() does not make copies of x and y in your global environment, it attaches a dataframe to the search path.

From ?attach:

The database is not actually attached.  Rather, a new environment
is created on the search path and the elements of a list
(including columns of a data frame) or objects in a save file or
an environment are _copied_ into the new environment. If you use
‘<<-’ or ‘assign’ to assign to an attached database, you only
alter the attached copy, not the original object. (Normal
assignment will place a modified version in the user's workspace:
see the examples.) For this reason ‘attach’ can lead to
confusion.

For example:

> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
> a <- data.frame(stuff=rnorm(100))
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
> attach(a)
> search()
[1] ".GlobalEnv" "a" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"
> rm(list=ls())
> search()
[1] ".GlobalEnv" "a" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"
> stuff
[1] -0.91436377 0.67397624 0.62891651 -0.99669584 2.07692590 -0.62702302
[...]
> detach(a)
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"

How do I remove everything from the environment in one go whilst preserving functions?

In RStudio, the Data items seems to be referring to data.frame/tibble/data.table objects. If that is the case, get the object names that data.frame and rm them

rm(list = ls()[sapply(ls(), \(x) inherits(get(x), 
what = c("data.frame", "list")))])

Or may use eapply

rm(list = names(which(unlist(eapply(.GlobalEnv, \(x) 
inherits(x, what = c("data.frame", "list")))))))

Update

Based on the OP's comments, if it is to only everything except functions

rm(list = names(which(!unlist(eapply(.GlobalEnv, 
\(x) inherits(x, what = "function"))))))

rm() doesn't seem to empty my R workspace

What you are seeing is the source code for the ls function. When you enter a function name without the parentheses, you'll see the complete source code for that function (provided that function is in one of the packages attached to the search path, or in the global environment).

When you see character(0) as the result of calling ls(), that means that there are no objects in the global environment. The base package, where ls calls home, is different from the global environment, and objects there cannot be removed.

When character(0) is the result of ls() after you call rm(list=ls()), you have successfully cleared the objects in the global environment.



Related Topics



Leave a reply



Submit