1keep <- function(..., list=character(0), all=FALSE, sure=FALSE) 2{ 3 if(missing(...) && missing(list)) 4 { 5 warning("keep something, or use rm(list=ls()) to clear workspace - ", 6 "nothing was removed") 7 return(invisible(NULL)) 8 } 9 names <- as.character(substitute(list(...)))[-1] 10 list <- c(list, names) 11 keep.elements <- match(list, ls(1,all.names=all)) 12 if(any(is.na(keep.elements))) 13 { 14 warning("you tried to keep \"", list[which(is.na(keep.elements))[1]], 15 "\" which doesn't exist in workspace - nothing was removed", sep="") 16 return(invisible(NULL)) 17 } 18 19 if(sure) 20 rm(list=ls(1,all.names=all)[-keep.elements], pos=1) 21 else 22 return(ls(1,all.names=all)[-keep.elements]) 23} 24