The general idiom for capturing what the user has set as an argument to a function is deparse(substitute(foo)) . This function is similar to the @Ian Ross function, but using this standard idiom:
ifrm <- function(obj, env = globalenv()) { obj <- deparse(substitute(obj)) if(exists(obj, envir = env)) { rm(list = obj, envir = env) } }
where I assume that you only want to remove objects from the global environment, therefore by default, but you can provide the environment through env . And here he is in action:
> a <- 1:10 > ls() [1] "a" "ifrm" > ifrm(a) > ls() [1] "ifrm" > ifrm(a) > ls() [1] "ifrm"
source share