| 1 |
|
| 2 |
retain <- function (..., list = character(0), envir = parent.frame(),force=FALSE) |
| 3 |
{ |
| 4 |
dots <- match.call(expand.dots = FALSE)$... |
| 5 |
if(environmentName(envir)==environmentName(globalenv()) && !force) { |
| 6 |
warning("retain will remove objects from global environment only if force=TRUE") |
| 7 |
return(invisible(NULL)) |
| 8 |
} |
| 9 |
if(!length(dots)) stop("at least one object has to be retained") |
| 10 |
if (!all(sapply(dots, function(x) is.symbol(x) || |
| 11 |
is.character(x)))) |
| 12 |
stop("... must contain names or character strings") |
| 13 |
names <- sapply(dots, as.character) |
| 14 |
if (length(names) == 0) |
| 15 |
names <- character(0) |
| 16 |
list <- .Primitive("c")(list, names) |
| 17 |
obs <- ls(envir=envir) |
| 18 |
if(!all(list %in% obs)) { |
| 19 |
mis <- setdiff(list,obs) |
| 20 |
mis <- paste(mis,collapse=", ") |
| 21 |
stop("Undefined object(s) ",mis) |
| 22 |
} |
| 23 |
to.remove <- obs[!(obs %in% list)] |
| 24 |
remove(list=to.remove, envir=envir, inherits=FALSE) |
| 25 |
} |