I would like to create a function (CleanEnvir) which basically calls remove/rm and which removes certain objects from .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()
ls()needs to look in the correct place. By default it looks in the current frame, that of the functionCleanEnvirin your case and hence was only finding"pattern"in your original.Which gives: