statquant statquant - 3 months ago 9
R Question

How can Iist only functions and those that come from a package

I use the

foreach
package to parallelize some stuff and I am tired of indicating 5 functions in
.export
everytime I need to use it.

I know I can do
foreach(...,.export=ls(.GlobalEnv))
but this transfers a lot of data to the workers and slow me down (there can be big tables defined).

So the question is how can I list only functions in the
.GlobalEnv


I did that:

getAllFunctions <- function(envir=.GlobalEnv){
allClasses <- sapply(grep(x=ls(envir), pattern='^%', value=TRUE, invert=TRUE), FUN=function(x){class(eval(parse(text=x)))})
fnNames <- names(allClasses)[allClasses == 'function']
return(fnNames)
}


But that's ugly (and gives everything) and I'm sure there is an idiomatic way

Answer

From the comments:

as.list(.GlobalEnv)[sapply(.GlobalEnv, is.function)]
Comments