This function selectively retains the objects specified in the Obj
parameter in the current environment, removing all other objects. It is
useful for memory management by clearing unnecessary objects from the
environment. The function also provides an option to print the names of the
kept and removed variables.
Usage
KeepOnly(Obj, Verbose = TRUE)
Arguments
- Obj
A character vector specifying the names of the objects to be kept
in the environment.
- Verbose
A logical value indicating whether to print the names of kept
and removed variables. Default to TRUE
.
Value
No return value, called for side effects.
Examples
A <- B <- C <- 15
ls()
#> [1] "A" "B" "C"
KeepOnly("A")
#> Removed Variables (2): 1:B || 2:C
#> Kept Variables (1): 1:A
ls()
#> [1] "A"
rm(list = ls())
A <- B <- C <- 15
KeepOnly(c("A","B"))
#> Removed Variables (1): 1:C
#> Kept Variables (2): 1:A || 2:B
ls()
#> [1] "A" "B"