remove all variables except functions

后端 未结 5 1403
耶瑟儿~
耶瑟儿~ 2020-12-12 09:43

I have loaded in a R console different type of objects. I can remove them all using

rm(list=ls())

or remove only the functions (but not the

相关标签:
5条回答
  • 2020-12-12 10:19

    The posted setdiff answer is nice. I just thought I'd post this related function I wrote a while back. Its usefulness is up to the reader :-).

    lstype<-function(type='closure'){ 
        inlist<-ls(.GlobalEnv)
        if (type=='function') type <-'closure'
        typelist<-sapply(sapply(inlist,get),typeof)
        return(names(typelist[typelist==type]))
    }
    
    0 讨论(0)
  • 2020-12-12 10:32

    You can use the following command to clear out ALL variables. Be careful because it you cannot get your variables back.

    rm(list=ls(all=TRUE))
    
    0 讨论(0)
  • 2020-12-12 10:37

    Here's a one-liner that removes all objects except for functions:

    rm(list = setdiff(ls(), lsf.str()))
    

    It uses setdiff to find the subset of objects in the global environment (as returned by ls()) that don't have mode function (as returned by lsf.str())

    0 讨论(0)
  • 2020-12-12 10:46

    Here's a pretty convenient function I picked up somewhere and adjusted a little. Might be nice to keep in the directory.

    list.objects <- function(env = .GlobalEnv) 
    {
        if(!is.environment(env)){
            env <- deparse(substitute(env))
            stop(sprintf('"%s" must be an environment', env))
        }
        obj.type <- function(x) class(get(x, envir = env))
        foo <- sapply(ls(envir = env), obj.type)
        object.name <- names(foo)
        names(foo) <- seq(length(foo))
        dd <- data.frame(CLASS = foo, OBJECT = object.name, 
                         stringsAsFactors = FALSE)
        dd[order(dd$CLASS),]
    }
    
    > x <- 1:5
    > d <- data.frame(x)
    > list.objects()
    #        CLASS       OBJECT
    # 1 data.frame            d
    # 2   function list.objects
    # 3    integer            x 
    > list.objects(env = x)
    # Error in list.objects(env = x) : "x" must be an environment
    
    0 讨论(0)
  • 2020-12-12 10:46

    I wrote this to remove all objects apart from functions from the current environment (Programming language used is R with IDE R-Studio):

        remove_list=c()                             # create a vector
    
          for(i in 1:NROW(ls())){                   # repeat over all objects in environment
            if(class(get(ls()[i]))!="function"){    # if object is *not* a function
             remove_list=c(remove_list,ls()[i])     # ..add to vector remove_list
             }    
          }
    
        rm(list=remove_list)                        # remove all objects named in remove_list
    

    Notes-

    The argument "list" in rm(list=) must be a character vector.

    The name of an object in position i of the current environment is returned from ls()[i] and the object itself from get(ls()[i]). Therefore the class of an object is returned from class(get(ls()[i]))

    0 讨论(0)
提交回复
热议问题