Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a vector with the name of all functions that one could use in R?

Tags:

I would like to have a call that returns me a vector with the names of all function that I could call in the current R session. Does anybody know how to achieve this?

(I would like to check user entered variables against this vector. We had some unforseen problem with users entering e.g., c as variable names)

UPDATE: I would like to get the function names from all packages currently loaded.

SOLUTION (half way): Based on Joris Meys tip with lsf.str() I came up with the following function that returns a sorted vector with all currently available function names:

getFunctionNames <- function() {     loaded <- (.packages())     loaded <- paste("package:", loaded, sep ="")     return(sort(unlist(lapply(loaded, lsf.str)))) } 

Bu,t see also the comments on Joris Meys' post for even better answers.

like image 673
Henrik Avatar asked Nov 24 '10 14:11

Henrik


People also ask

How do I find the vector of a function in R?

function() in R, allows you to convert a non-vector object into a vector. For example, if you have a matrix as an object and you wanted to convert it into a vector, then you can use the as. vector() function to get this done.

Which of the following functions is used to return a vector in R?

The is. vector() function takes an object as an input and returns TRUE if the input object is a vector.

Which function is used to give a name to the elements of a vector?

names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object.

What does vector () do in R?

vector() allows any type (see typeof ) for mode , and when mode is not "any" , is. vector(x, mode) is almost the same as typeof(x) == mode . a non-negative integer specifying the desired length. For a long vector, i.e., length > .


2 Answers

I'd use lsf.str() as a start.

eg : x <- as.character(lsf.str("package:base")) gives you a list of all functions in the base package. You could do add all packages you want to check against. stats and utils come to mind first.

EDIT : Regarding your question about currently loaded packages :

x <- unlist(sapply(search()[-1],function(x)as.character(lsf.str(x)))) see comments

pkgs <- search() pkgs <- pkgs[grep("package:",pkgs)] y <- unlist(sapply(pkgs,lsf.str)) 

does the trick.

like image 63
Joris Meys Avatar answered Sep 24 '22 00:09

Joris Meys


I asked a similar Q on R-Help many moons ago (2007) and Prof. Brian Ripley provided this as a solution:

findfuns <- function(x) {      if(require(x, character.only=TRUE)) {         env <- paste("package", x, sep=":")         nm <- ls(env, all=TRUE)         nm[unlist(lapply(nm, function(n) exists(n, where=env,                                                mode="function",                                                inherits=FALSE)))]      } else character(0) } pkgs <- dir(.Library) z <-  lapply(pkgs, findfuns) names(z) <- pkgs Z <- sort(unique(unlist(z))) 

Which gives output like:

> head(Z) [1] "^"        "-"        "-.Date"   "-.POSIXt" ":"        "::" 

This was for finding all the functions in packages specified by object pkgs so you can control which packages are loaded/checked against.

A modified version that work on the currently loaded set of packages would be:

findfuns2 <- function(pkgs) {     nm <- ls(pkgs, all = TRUE)     nm[unlist(lapply(nm, function(n) exists(n, where = pkgs,                                             mode = "function",                                             inherits = FALSE)))]     if(isTRUE(all.equal(length(nm), 0)))         character(0)     else         nm }  pkgs <- search() pkgs <- pkgs[grep("package:", pkgs)] z <- lapply(pkgs, findfuns2) z <- sort(unique(unlist(z))) head(z) 
like image 21
Gavin Simpson Avatar answered Sep 23 '22 00:09

Gavin Simpson