查找当前工作空间中的所有函数

时间:2022-02-01 19:00:13

I'd like to find all functions in my current workspace and thought about using is.function for that purpose.

我想在当前工作空间中找到所有函数,并考虑使用is.function来实现此目的。

The "problem" is that is.function expects a true R object, not the character string name of an object.

“问题”是is.function需要一个真正的R对象,而不是对象的字符串名称。

That's my solution, but using eval(substitute(...)) seems a bit involved. Any ideas for a more straight forward way or is that the only way this can be done?

这是我的解决方案,但使用eval(替代(...))似乎有点牵扯。任何想法更直接的方式或是唯一的方法可以做到这一点?

Example content

x <- TRUE
y <- 10
foo1 <- function() message("hello world!")
foo2 <- function() message("hello world again!")

Finding all function objects

查找所有函数对象

wscontent <- ls(all.names=TRUE)
funs <- sapply(wscontent, function(ii) {
    eval(substitute(is.function(OBJ), list(OBJ=as.name(ii))))
})
> funs
     foo1      foo2      funs wscontent         x         y 
     TRUE      TRUE     FALSE     FALSE     FALSE     FALSE 

4 个解决方案

#1


9  

How about

lsf.str()

which should list all functions.

哪个应列出所有功能。

#2


1  

funs <- sapply(wscontent, function(x) is.function(get(x)))

#3


1  

I wrote a more general toy a while back:

我不久前写了一个更普通的玩具:

lstype<-function(type='closure'){
inlist<-ls(.GlobalEnv)
if (type=='function') type <-'closure'
typelist<-sapply(sapply(inlist,get),typeof)
return(names(typelist[typelist==type]))
}

#4


0  

You can use eapply:

你可以使用eapply:

which(unlist(eapply(.GlobalEnv, is.function)))

... or sapply with as.list:

...或者使用as.list:

which(sapply(as.list(.GlobalEnv), is.function))

#1


9  

How about

lsf.str()

which should list all functions.

哪个应列出所有功能。

#2


1  

funs <- sapply(wscontent, function(x) is.function(get(x)))

#3


1  

I wrote a more general toy a while back:

我不久前写了一个更普通的玩具:

lstype<-function(type='closure'){
inlist<-ls(.GlobalEnv)
if (type=='function') type <-'closure'
typelist<-sapply(sapply(inlist,get),typeof)
return(names(typelist[typelist==type]))
}

#4


0  

You can use eapply:

你可以使用eapply:

which(unlist(eapply(.GlobalEnv, is.function)))

... or sapply with as.list:

...或者使用as.list:

which(sapply(as.list(.GlobalEnv), is.function))