Byron Dom
2014-May-18 22:49 UTC
[R] Is there an easier way than this to list all functions in the global environment?
After an unsuccessful search thru several books plus online documentation, I was unable to find a simple way to do this, so I wrote my own function (see below) to do it. ?I'm relatively new to R however, so I'm guessing that there must be an easier way to do it. I want to list all functions and only the functions (not other objects also) in the global environment. I'm working with several old R workspaces that contain a very large number of objects -- functions, data structures, etc. and I would to be able to easily find out which objects are functions. The function below (listfunc()) works fine. I'm just asking this for future reference. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ listfunc <- function() { # lists names of all function objects in the global environment obj <- objects(.GlobalEnv) funclist <- character(length = 0) for (i in 1:length(obj)) { ? ? if (mode(get(obj[i])) == "function") funclist <- c(funclist,obj[i]) ? } return(funclist) } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Peter Alspach
2014-May-18 23:24 UTC
[R] Is there an easier way than this to list all functions in the global environment?
Tena koe Byron Many years ago, I came across the following function by Simon Fear: function (splitby = mode, pos = 1, ...) { lsout <- ls(pos = pos, ...) tapply(lsout, sapply(lsout, function(x) { splitby(get(x)) }), invisible) } from which one can deduce that, given your obj, the following line could replace your loop: obj[sapply(obj, function(x) mode(get(x))=='function')] For future reference, it can be slow in R to continually append to an object: test <- character(0) system.time(for (i in 1:10^5) test <- c(test, as.character(i))) # user system elapsed # 47.86 0.02 47.89 test <- character(10^5) system.time(for (i in 1:10^5) test[i] <- as.character(i)) # user system elapsed # 0.25 0.00 0.25 HTH .... Peter Alspach -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Byron Dom Sent: Monday, 19 May 2014 10:50 a.m. To: "r-help at r-project.org" Subject: [R] Is there an easier way than this to list all functions in the global environment? After an unsuccessful search thru several books plus online documentation, I was unable to find a simple way to do this, so I wrote my own function (see below) to do it. ?I'm relatively new to R however, so I'm guessing that there must be an easier way to do it. I want to list all functions and only the functions (not other objects also) in the global environment. I'm working with several old R workspaces that contain a very large number of objects -- functions, data structures, etc. and I would to be able to easily find out which objects are functions. The function below (listfunc()) works fine. I'm just asking this for future reference. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ listfunc <- function() { # lists names of all function objects in the global environment obj <- objects(.GlobalEnv) funclist <- character(length = 0) for (i in 1:length(obj)) { ? ? if (mode(get(obj[i])) == "function") funclist <- c(funclist,obj[i]) ? } return(funclist) } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. The contents of this e-mail are confidential and may be ...{{dropped:14}}