我如何从R的工作区中移除所有的对象?

时间:2021-05-18 07:42:44

I have a workspace with lots of objects and I would like to remove all but one. Ideally I would like to avoid having to type rm(obj.1, obj.2... obj.n). Is it possible to indicate remove all objects but these ones?

我有一个有很多对象的工作空间,我想删除除一个以外的所有东西。理想情况下,我希望避免使用rm(obj)。1、obj.2……obj.n)。是否可以指示删除所有对象,但这些对象?

13 个解决方案

#1


243  

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

这里有一个简单的构造,使用setdiff:

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

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

和一个完整的示例。在你自己的风险下运行它——它将删除除x之外的所有变量:

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

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

ls()
[1] "x"

#2


38  

Using the keep function from the gdata package is quite convenient.

使用gdata包中的keep函数非常方便。

> ls()
[1] "a" "b" "c"

library(gdata)
> keep(a) #shows you which variables will be removed
[1] "b" "c"
> keep(a, sure = TRUE) # setting sure to TRUE removes variables b and c
> ls()
[1] "a"

#3


32  

I think another option is to open workspace in RStudio and then change list to grid at the top right of the environment(image below). Then tick the objects you want to clear and finally click on clear.

我认为另一个选择是在RStudio中打开工作区,然后在环境的右上方将列表更改为网格(下图)。然后勾选你想清除的对象,最后点击clear。

我如何从R的工作区中移除所有的对象?

#4


15  

To keep all objects whose names match a pattern, you could use grep, like so:

要保留所有名称与模式匹配的对象,可以使用grep,如下所示:

to.remove <- ls()
to.remove <- c(to.remove[!grepl("^obj", to.remove)], "to.remove")
rm(list=to.remove)

#5


12  

Replace v with the name of the object you want to keep

用要保存的对象的名称替换v。

rm(list=(ls()[ls()!="v"]))

hat-tip: http://r.789695.n4.nabble.com/Removing-objects-and-clearing-memory-tp3445763p3445865.html

速溶咖啡:http://r.789695.n4.nabble.com/Removing-objects-and-clearing-memory-tp3445763p3445865.html

#6


8  

I just spent several hours hunting for the answer to a similar but slightly different question - I needed to be able to delete all objects in R (including functions) except a handful of vectors.

我花了几个小时寻找一个类似但略有不同的问题的答案——我需要能够删除R(包括函数)中的所有对象,除了少数几个向量。

One way to do this:

一种方法是:

rm(list=ls()[! ls() %in% c("a","c")])

Where the vectors that I want to keep are named 'a' and 'c'.

我想要保留的向量叫做a和c。

Hope this helps anyone searching for the same solution!

希望这能帮助任何人寻找相同的解决方案!

#7


4  

This takes advantage of ls()'s pattern option, in the case you have a lot of objects with the same pattern that you don't want to keep:

这利用了ls()的模式选项,在这种情况下,您有许多对象的模式与您不想保留的模式相同:

> foo1 <- "junk"; foo2 <- "rubbish"; foo3 <- "trash"; x <- "gold"  
> ls()
[1] "foo1" "foo2" "foo3" "x"   
> # Let's check first what we want to remove
> ls(pattern = "foo")
[1] "foo1" "foo2" "foo3"
> rm(list = ls(pattern = "foo"))
> ls()
[1] "x"

#8


1  

From within a function, rm all objects in .GlobalEnv except the function

从一个函数中,rm all对象在.GlobalEnv中除了函数。

initialize <- function(country.name) {

  if (length(setdiff(ls(pos = .GlobalEnv), "initialize")) > 0) {
    rm(list=setdiff(ls(pos = .GlobalEnv), "initialize"), pos = .GlobalEnv)
  }

}

#9


1  

require(gdata)
keep(object_1,...,object_n,sure=TRUE)
ls()

#10


0  

How about this?

这个怎么样?

# Removes all objects except the specified & the function itself.

rme <- function(except=NULL){
  except = ifelse(is.character(except), except, deparse(substitute(except)))
  rm(list=setdiff(ls(envir=.GlobalEnv), c(except,"rme")), envir=.GlobalEnv)
}

#11


0  

assuming you want to remove every object except df from environment:

假设您想删除除df以外的所有对象:

rm(list = ls(pattern="[^df]"))

#12


0  

let's think in different way, what if we wanna remove a group? try this,

让我们用不同的方式思考,如果我们想要移除一个组呢?试试这个,

 rm(list=ls()[grep("xxx",ls())]) 

I personally don't like too many tables, variables on my screen, yet I can't avoid using them. So I name the temporary things starting with "xxx", so I can remove them after it is no longer used.

我个人不喜欢太多的表,我的屏幕上的变量,但是我不能避免使用它们。所以我用“xxx”来命名临时的东西,这样我就可以在它不再使用后删除它们。

#13


-4  

The following will remove all the objects from your console

下面将从您的控制台上删除所有的对象。

rm(list = ls())

#1


243  

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

这里有一个简单的构造,使用setdiff:

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

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

和一个完整的示例。在你自己的风险下运行它——它将删除除x之外的所有变量:

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

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

ls()
[1] "x"

#2


38  

Using the keep function from the gdata package is quite convenient.

使用gdata包中的keep函数非常方便。

> ls()
[1] "a" "b" "c"

library(gdata)
> keep(a) #shows you which variables will be removed
[1] "b" "c"
> keep(a, sure = TRUE) # setting sure to TRUE removes variables b and c
> ls()
[1] "a"

#3


32  

I think another option is to open workspace in RStudio and then change list to grid at the top right of the environment(image below). Then tick the objects you want to clear and finally click on clear.

我认为另一个选择是在RStudio中打开工作区,然后在环境的右上方将列表更改为网格(下图)。然后勾选你想清除的对象,最后点击clear。

我如何从R的工作区中移除所有的对象?

#4


15  

To keep all objects whose names match a pattern, you could use grep, like so:

要保留所有名称与模式匹配的对象,可以使用grep,如下所示:

to.remove <- ls()
to.remove <- c(to.remove[!grepl("^obj", to.remove)], "to.remove")
rm(list=to.remove)

#5


12  

Replace v with the name of the object you want to keep

用要保存的对象的名称替换v。

rm(list=(ls()[ls()!="v"]))

hat-tip: http://r.789695.n4.nabble.com/Removing-objects-and-clearing-memory-tp3445763p3445865.html

速溶咖啡:http://r.789695.n4.nabble.com/Removing-objects-and-clearing-memory-tp3445763p3445865.html

#6


8  

I just spent several hours hunting for the answer to a similar but slightly different question - I needed to be able to delete all objects in R (including functions) except a handful of vectors.

我花了几个小时寻找一个类似但略有不同的问题的答案——我需要能够删除R(包括函数)中的所有对象,除了少数几个向量。

One way to do this:

一种方法是:

rm(list=ls()[! ls() %in% c("a","c")])

Where the vectors that I want to keep are named 'a' and 'c'.

我想要保留的向量叫做a和c。

Hope this helps anyone searching for the same solution!

希望这能帮助任何人寻找相同的解决方案!

#7


4  

This takes advantage of ls()'s pattern option, in the case you have a lot of objects with the same pattern that you don't want to keep:

这利用了ls()的模式选项,在这种情况下,您有许多对象的模式与您不想保留的模式相同:

> foo1 <- "junk"; foo2 <- "rubbish"; foo3 <- "trash"; x <- "gold"  
> ls()
[1] "foo1" "foo2" "foo3" "x"   
> # Let's check first what we want to remove
> ls(pattern = "foo")
[1] "foo1" "foo2" "foo3"
> rm(list = ls(pattern = "foo"))
> ls()
[1] "x"

#8


1  

From within a function, rm all objects in .GlobalEnv except the function

从一个函数中,rm all对象在.GlobalEnv中除了函数。

initialize <- function(country.name) {

  if (length(setdiff(ls(pos = .GlobalEnv), "initialize")) > 0) {
    rm(list=setdiff(ls(pos = .GlobalEnv), "initialize"), pos = .GlobalEnv)
  }

}

#9


1  

require(gdata)
keep(object_1,...,object_n,sure=TRUE)
ls()

#10


0  

How about this?

这个怎么样?

# Removes all objects except the specified & the function itself.

rme <- function(except=NULL){
  except = ifelse(is.character(except), except, deparse(substitute(except)))
  rm(list=setdiff(ls(envir=.GlobalEnv), c(except,"rme")), envir=.GlobalEnv)
}

#11


0  

assuming you want to remove every object except df from environment:

假设您想删除除df以外的所有对象:

rm(list = ls(pattern="[^df]"))

#12


0  

let's think in different way, what if we wanna remove a group? try this,

让我们用不同的方式思考,如果我们想要移除一个组呢?试试这个,

 rm(list=ls()[grep("xxx",ls())]) 

I personally don't like too many tables, variables on my screen, yet I can't avoid using them. So I name the temporary things starting with "xxx", so I can remove them after it is no longer used.

我个人不喜欢太多的表,我的屏幕上的变量,但是我不能避免使用它们。所以我用“xxx”来命名临时的东西,这样我就可以在它不再使用后删除它们。

#13


-4  

The following will remove all the objects from your console

下面将从您的控制台上删除所有的对象。

rm(list = ls())