My memory is getting clogged by a bunch of intermediate files (call them temp1
, temp2
, etc.), and I would like to know if it is possible to remove them from memory without doing repeated rm
calls (i.e. rm(temp1)
, rm(temp2)
)?
I tried rm(list(temp1, temp2, etc.))
, but that doesn't seem to work.
The ls() code lists all of the objects in your workspace. The rm() code removes objects in your workspace. You can begin your code with the rm() function to clear all of the objects from your workspace to start with a clean environment.
rm() function in R Language is used to delete objects from the memory. It can be used with ls() function to delete all objects.
You can do both by restarting your R session in RStudio with the keyboard shortcut Ctrl+Shift+F10 which will totally clear your global environment of both objects and loaded packages.
The rm() function in R is used to delete or remove a variable from a workspace.
Make the list a character vector (not a vector of names)
rm(list = c('temp1','temp2'))
or
rm(temp1, temp2)
An other solution rm(list=ls(pattern="temp"))
, remove all objects matching the pattern.
Or using regular expressions
"rmlike" <- function(...) {
names <- sapply(
match.call(expand.dots = FALSE)$..., as.character)
names = paste(names,collapse="|")
Vars <- ls(1)
r <- Vars[grep(paste("^(",names,").*",sep=""),Vars)]
rm(list=r,pos=1)
}
rmlike(temp)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With