I am trying to create a function to remove all but one to three items from the global environment. I am able to do it for one but not for two, three or more. Would appreciate any help on this using base R or rlang
a <- "a"
b <- c(1,2,3,4)
c <- c("M", "F")
remove_all_but <- function(x){
Sx <- deparse(substitute(x))
rm(list=Sx,envir=sys.frame(-1))
}
remove_all_but(a)
remove_all_but(c(a, b))
#> Warning in rm(list = Sx, envir = sys.frame(-1)): object 'c(a, b)' not found