Two incredibly useful functions to throw into your .rprofile
by Karthik Ram. Average Reading Time: about a minute.
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]="class" names(foo)[2]="object" 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.
You might enjoy a similar function that lists the size of the objects in memory: http://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session
And the most popular Rprofile functions on stackoverflow: http://stackoverflow.com/questions/1189759/expert-r-users-whats-in-your-rprofile