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.

To leave a comment for the author, please follow the link and comment on his blog: Inundata » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.