从函数中删除.GlobalEnv中的对象

时间:2021-08-08 23:22:55

I would like to create a function (CleanEnvir) which basically calls remove/rm and which removes certain objects from .GlobalEnv.

我想创建一个函数(CleanEnvir),它基本上调用remove / rm,并从.GlobalEnv中删除某些对象。

  CleanEnvir <- function(pattern = "tmp"){
      rm(list = ls()[grep("tmp", ls())], envir = globalenv())
  }

  keep <- 1
  tmp.to.be.removed <- 0
  ls()

  ## does not work
  CleanEnvir()
  ls()

  ## does work
  rm(list = ls()[grep("tmp", ls())], envir = globalenv())
  ls()

3 个解决方案

#1


18  

ls() needs to look in the correct place. By default it looks in the current frame, that of the function CleanEnvir in your case and hence was only finding "pattern" in your original.

ls()需要查看正确的位置。默认情况下,它会查看当前帧,即您的案例中的CleanEnvir函数,因此只能在原始文件中找到“pattern”。

CleanEnvir <- function(pattern = "tmp") {
    objs <- ls(pos = ".GlobalEnv")
    rm(list = objs[grep("tmp", objs)], pos = ".GlobalEnv")
}

Which gives:

这使:

> CleanEnvir <- function(pattern = "tmp") {
+     objs <- ls(pos = ".GlobalEnv")
+     rm(list = objs[grep("tmp", objs)], pos = ".GlobalEnv")
+ }
> ls()
[1] "CleanEnvir"        "foo"               "keep"             
[4] "tmp.to.be.removed"
> CleanEnvir()
> ls()
[1] "CleanEnvir" "foo"        "keep"

#2


7  

You need to do your search in the Global Env as well as the removal there:

您需要在Global Env中进行搜索以及在那里删除:

CleanEnvir <- function(pattern = "tmp"){
      rm(list = ls(envir=globalenv())[
             grep("tmp", ls(envir=globalenv()))], envir = globalenv())
  }

#3


2  

Shortest code solution I got for this is this one:

我得到的最短代码解决方案就是这个:

remove a specific variable:

删除特定变量:

y <- TRUE

CleanEnvir <- function(x) {rm(list=deparse(substitute(x)),envir=.GlobalEnv)}

CleanEnvir(y)
y

deparse substitute to paste the variable name rather than its value, and indeed pos = ".GlobalEnv" works, but you can also simply use envir=.GlobalEnv

deparse替换来粘贴变量名而不是它的值,实际上pos =“。GlobalEnv”可以工作,但你也可以简单地使用envir = .GlobalEnv

SOLUTION 2: This actually allows for pattern matching. (I STRONGLY recommend against this because you could possibly remove stuff you don't want to remove by accident. I.e. you want to remove tmp1 and tmp2 but you forgot that there is another variable that is called Global.tmp and localtmp as in temperature for example.

解决方案2:这实际上允许模式匹配。 (我强烈建议不要这样做,因为你可能会删除你不想意外删除的东西。你想要删除tmp1和tmp2,但你忘记了另一个名为Global.tmp和localtmp的变量,因为温度为例。

remove by pattern:

按模式删除:

myvar1 <- TRUE
myvar2 <- FALSE 

Pat.clean.Envir <- function(x) { rm(list = ls(.GlobalEnv)[grep(deparse(substitute(x)), ls(.GlobalEnv))], envir = .GlobalEnv) }

Pat.clean.Envir(myvar)

cheers.

干杯。

#1


18  

ls() needs to look in the correct place. By default it looks in the current frame, that of the function CleanEnvir in your case and hence was only finding "pattern" in your original.

ls()需要查看正确的位置。默认情况下,它会查看当前帧,即您的案例中的CleanEnvir函数,因此只能在原始文件中找到“pattern”。

CleanEnvir <- function(pattern = "tmp") {
    objs <- ls(pos = ".GlobalEnv")
    rm(list = objs[grep("tmp", objs)], pos = ".GlobalEnv")
}

Which gives:

这使:

> CleanEnvir <- function(pattern = "tmp") {
+     objs <- ls(pos = ".GlobalEnv")
+     rm(list = objs[grep("tmp", objs)], pos = ".GlobalEnv")
+ }
> ls()
[1] "CleanEnvir"        "foo"               "keep"             
[4] "tmp.to.be.removed"
> CleanEnvir()
> ls()
[1] "CleanEnvir" "foo"        "keep"

#2


7  

You need to do your search in the Global Env as well as the removal there:

您需要在Global Env中进行搜索以及在那里删除:

CleanEnvir <- function(pattern = "tmp"){
      rm(list = ls(envir=globalenv())[
             grep("tmp", ls(envir=globalenv()))], envir = globalenv())
  }

#3


2  

Shortest code solution I got for this is this one:

我得到的最短代码解决方案就是这个:

remove a specific variable:

删除特定变量:

y <- TRUE

CleanEnvir <- function(x) {rm(list=deparse(substitute(x)),envir=.GlobalEnv)}

CleanEnvir(y)
y

deparse substitute to paste the variable name rather than its value, and indeed pos = ".GlobalEnv" works, but you can also simply use envir=.GlobalEnv

deparse替换来粘贴变量名而不是它的值,实际上pos =“。GlobalEnv”可以工作,但你也可以简单地使用envir = .GlobalEnv

SOLUTION 2: This actually allows for pattern matching. (I STRONGLY recommend against this because you could possibly remove stuff you don't want to remove by accident. I.e. you want to remove tmp1 and tmp2 but you forgot that there is another variable that is called Global.tmp and localtmp as in temperature for example.

解决方案2:这实际上允许模式匹配。 (我强烈建议不要这样做,因为你可能会删除你不想意外删除的东西。你想要删除tmp1和tmp2,但你忘记了另一个名为Global.tmp和localtmp的变量,因为温度为例。

remove by pattern:

按模式删除:

myvar1 <- TRUE
myvar2 <- FALSE 

Pat.clean.Envir <- function(x) { rm(list = ls(.GlobalEnv)[grep(deparse(substitute(x)), ls(.GlobalEnv))], envir = .GlobalEnv) }

Pat.clean.Envir(myvar)

cheers.

干杯。