# Two incredibly useful functions to throw into your .rprofile

February 7, 2012
By

(This article was first published on Inundata » R, and kindly contributed to R-bloggers)

I’ve neglected this blog for quite some time but I’m getting around to finishing up a bunch of draft posts. But here is a quick one:

Listing objects in your global environment
A simple ls() doesn’t really tell you enough useful information at a glance. Most often I just want to know what I named certain data.frames or functions. This handy little function, called as lsa() will do that for you:

lsa <- function()
{
obj_type <- function(x) { class(get(x)) }
foo=data.frame(sapply(ls(envir=.GlobalEnv),obj_type))
foo\$object_name=rownames(foo)
names(foo)[1]=&quot;class&quot;
names(foo)[2]=&quot;object&quot;
return(unrowname(foo))
}


Listing all functions in a certain package
This can be called with lsp(). The pattern argument will allow you to quickly find the right function if you vaguely remember the name.

lsp <-function(package, all.names = FALSE, pattern)
{
package <- deparse(substitute(package))
ls(
pos = paste("package", package, sep = ":"),
all.names = all.names,
pattern = pattern
)
}


Be sure to throw them both in a new environment (i.e. not the global one) so they don’t get accidentally removed when you clear your variables.