How to use the rm function without erasing everything, leaving only one or two vectors?

Asked

Viewed 72 times

2

I don’t want to exclude vec1 and a from my Workspace. I tried

vec1 <- c(1,2,3)    
vec2 <- c(2,3)    
a <- 2    
b <- 3    
x <- vec1    
y <- a    
   
cleanmem <- function(x,y){    
  mem = vector()    
  mem <- ls(all.names = T)    
  mem <- mem[which(mem != "x" & mem != "y" )]    
  rm(list=(mem));gc()
}    
cleanmem(x,y);ls()

or

cleanmem <- function(x,y){
list = ls(all.names = T)
list <- list[which(list != "x" & list != "y" )]
rm(list)
gc()
}
cleanmem(x,y)
ls()

I also tried to

cleanmem <- function(x,y){
    rm(list = ls()[which(list != "x" & list != "y" )])
    gc()
}
cleanmem(x,y)

2 answers

2


The problem is the environments. Each R function creates an environment for you and both ls how much rm act by default in the current environment.

To affect the global environment you have to specify it directly. To do this you can add an environment parameter to the code. I made the change in the first example with a function receiving strings and one receiving arguments with ....
Note: remember that in this way the function itself is also deleted. All objects in the global Nvironment are deleted, including other functions you created before in the code.

vec1 <- c(1,2,3)    
vec2 <- c(2,3)    
a <- 2    
b <- 3    

environment()
#> <environment: R_GlobalEnv>

test_function <- function() print(environment())

test_function()
#> <environment: 0x2571008>

cleanmem_general <- function(to_keep = c(), target_env = .GlobalEnv){    
  all_vars <- ls(all.names = T, envir = target_env)    
  to_remove <- all_vars[which(!(all_vars %in% to_keep))]    
  rm(list = to_remove, pos = target_env);gc()
}    

cleanmem_general(c("vec1", "a"));ls()
#>          used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 486124 26.0    1050760 56.2   676331 36.2
#> Vcells 894859  6.9    8388608 64.0  1820804 13.9
#> [1] "a"    "vec1"

vec1 <- c(1,2,3)    
vec2 <- c(2,3)    
a <- 2    
b <- 3    

cleanmem_nse <- function(..., target_env = .GlobalEnv){
  to_keep <- as.character(substitute(list(...)))[-1]
  all_vars <- ls(all.names = T, envir = target_env)    
  to_remove <- all_vars[which(!(all_vars %in% to_keep))]    
  rm(list = to_remove, pos = target_env);gc()
}    

cleanmem_nse(vec1, a);ls()
#>          used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 486215 26.0    1050760 56.2   676331 36.2
#> Vcells 894943  6.9    8388608 64.0  1820804 13.9
#> [1] "a"    "vec1"

Created on 2020-06-15 by the reprex package (v0.3.0)

  • That blz Jorge Mendes, thank you very much. But we could declare vec1 and the inside cleanmem_modified(x,y). In this case solved it is necessary to do before the function x <- vec1. And when going through cleanmem_modified(x,y), call vec1 <- x. It already solves, but we would have left more optimized?

  • I understood, I had only attacked myself to the main problem of environments. I will make a modification with this.

  • I could only update now @Fabianobrião. but there is something more general

  • So, I turned myself on to it, that the function would be erased. I think I’ll invest in a really big memory card. kkkk, I am entering the area of Data Science, I am a math teacher in IES 10 years and now the thing has become very black. So, as I already did some work with R when someone nearby asked, I will deepen until the end of the year, but I will migrate to Python, which I think has much more opportunities). I am already a mathematician/statistician by profession, let’s see how life is now changing area. Thank you, hug.

1

To remove all but one object:

rm(list = ls()[-a])

To remove all but some objects:

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

# ou

rm(list = setdiff(ls(), c("a", "b")))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.