Home > R > Exploring the functions in a package

Exploring the functions in a package

Sometimes it can be useful to list all the functions inside a package. This is done in the same way that you would list variables in your workspace. That is, using ls. The syntax is ls(pos = "package:packagename"), which is easy enough if you can remember it. Unfortunately, I never can, and have to type search() first to see what the format of that string is.

Today, that problem is solved with a tiny utility function to save remembering things, and to save typing.

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

all.names and pattern behave in the same way as they do in regular ls. You use it like this:

lsp(base, TRUE)
lsp(base, pattern = "^is")

EDIT: I’ve had a couple of questions about the use case, and there are some interesting comments on alternatives. My thinking behind this function was that I sometimes know I’ve seen a function in a package but can’t remember what it’s called. If you can hazard a guess at the name, then apropos is probably better, though it looks everywhere on the search path rather than in a particular package. Autocompletion is also useful for this, but you need to know the first few characters of what you are looking for. (Activate autocompletions by pressing TAB in R GUI or Rstudio or CTRL+space in eclipse. I can’t remember what the shortcut is in emacs, but you probably just mash CTRL+META until you have RSI.) Finally, the unknownR package is useful for finding new functions that you hadn’t heard of yet.

  1. 27th January, 2012 at 9:07 am

    Nice – but I am sure that you know that you can view all exported functions of a package by typing e.g.

    base:: TAB TAB


    base::: TAB TAB

    to see all including non-exported functions?

    Much faster and very easy to remember. In addition: by using :: yiou can sirectkly use that function, and the same applies for ::: if you want to use non-exported functions.



    • 27th January, 2012 at 11:15 am

      Yeah, autocompletion is great, and I use that as well. The R GUI autocompletion only lists the first 10 or so results, so I find it’s better for completion than discovery (unless you know the first few characters of what you are looking for).

      Also, autocompletion varies between development environments. I mostly use eclipse these days, and it doesn’t limit completions to a particular package, even if you explcitily type ::.

  2. 27th January, 2012 at 9:39 am

    There is also a package to help you discover functions you didn’t know you didn’t know. Quite useful to discover new functions… It’s called unknownR (http://cran.r-project.org/web/packages/unknownR/index.html) and has been presented at LondonR (http://www.londonr.org/unknownR.pdf).

    • 27th January, 2012 at 11:27 am

      Yes. unknownR is very useful, though maybe for a different use case. That’s more about learning R, whereas as lsp is about “I’m sure utils had a function for getting 8 character DOS names, but I can’t remember what it was called.” So you type lsp(utils), and a quick perusal of the results reveals shortPathNames.

  3. 16th July, 2013 at 9:47 am


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: