0

I've extracted the function for rm() in R (because I'm annoyed at having to type rm(list=ls()) everytime I want to clear my environment; it get's dirty fast) and I've tried modifying it to automatically use the list=ls() code but it hasn't worked. Can anyone else figure out a way to edit this function to automatically do what rm(list=ls()) does but not delete itself.

function (..., list = character(), pos = -1, envir = as.environment(pos), 
    inherits = FALSE) 
{
    dots <- match.call(expand.dots = FALSE)$...
    if (length(dots) && !all(sapply(dots, function(x) is.symbol(x) || 
        is.character(x)))) 
        stop("... must contain names or character strings")
    names <- sapply(dots, as.character)
    if (length(names) == 0L) 
        names <- character()
    list <- .Primitive("c")(list, names)
    .Internal(remove(list, envir, inherits))
}

FYI: clr <- rm(list=ls()) doesn't work either.

timothy.s.lau
  • 1,001
  • 1
  • 10
  • 22
  • `f <- function() with(.GlobalEnv, rm(list=ls()))` Is that what you're trying to do? – Rich Scriven Sep 24 '14 at 05:38
  • @Pascal I think it isn't a duplicate because I'm creating a new function rather than using rm(); although that solution does seem to have a similar result. Really all that needs to be done to keep it from being deleted, apparently, is to add a "." before the function name, let's call it ".clr" then. My question remains the same. How do you edit the function above. – timothy.s.lau Sep 24 '14 at 05:40
  • Unless you know how to use `.Internal()` that's going to be pretty hard – Rich Scriven Sep 24 '14 at 05:41
  • 1
    Something like `myrm<-function() rm(list=setdiff(ls(envir=.GlobalEnv),"myrm"),envir=.GlobalEnv)` should work – nicola Sep 24 '14 at 05:45
  • @timothy.s.lau You are more asking others to create it for you. –  Sep 24 '14 at 05:47
  • The answer is: ``function(){vector1 <- setdiff(ls(envir = .GlobalEnv), lsf.str(envir = .GlobalEnv)[]); rm(list = vector1, pos = 1)}`` – timothy.s.lau Sep 30 '14 at 19:13

0 Answers0