Two incredibly useful functions to throw into your .rprofile

[This article was first published on Inundata » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


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.

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

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)