简体   繁体   English

删除除函数之外的所有变量

[英]remove all variables except functions

I have loaded in a R console different type of objects.我在 R 控制台中加载了不同类型的对象。 I can remove them all using我可以使用它们全部删除

rm(list=ls())

or remove only the functions (but not the variables) using或仅删除函数(而不是变量)使用

rm(list=lsf.str())

My question is: is there a way to remove all variables except the functions我的问题是:有没有办法删除除函数之外的所有变量

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() )它使用setdiff来查找全局环境中没有模式function的对象子集(由ls()返回ls() (由lsf.str()返回)

The posted setdiff answer is nice.发布的setdiff答案很好。 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]))
}

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))

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

I wrote this to remove all objects apart from functions from the current environment (Programming language used is R with IDE R-Studio):我写这个是为了从当前环境中删除除函数之外的所有对象(使用的编程语言是带有 IDE R-Studio 的 R):

    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. rm(list=) 中的参数“list”必须是字符向量。

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]).当前环境中位置 i 中的对象名称从 ls()[i] 返回,对象本身从 get(ls()[i]) 返回。 Therefore the class of an object is returned from class(get(ls()[i]))因此,对象的类是从 class(get(ls()[i])) 返回的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM