R tips I wish I had learned earlier – Getting Help in R

Note: Earlier this week I solicited advice using twitter and the rstats forum of the website reddit, asking experienced users of R what tips or tricks they wish they had learned earlier in their careers. I’m summarizing and expanding on the responses here. I want to say a big thank you to everyone who responded, I learned some new things myself.

It used to be a huge pain to search for something like ‘R linear regression’ because the letter R wasn’t recognized as something that referred to the R language. It would give you anything with a registered trade mark, anything with a single letter R anywhere in the text.  Annoying.  Now, according to some users on twitter, things are better, but it still makes things a lot easier to know how and where to search for help in R.

Finding Packages and Functions

Starting within R you can use either the single or double question mark:

?lm gives you help on a specific function (in this case, lm)

??term searches all the package help files to look for a ‘fuzzy match’.  If you want to use multiple words you can put the term in quotes.

apropos('term') returns a vector with all functions that contain the string ‘term’.

If you are searching for something like ‘plot’ you’re going to get a lot of results. Searching for ‘harmonics’ will give you less, but you’re stuck with only the help descriptions, so you’ve got to do some legwork to find the function that works best for you.

The next option is to use some kind of Internet search engine. Searching “R linear regression” does return some hits, but it’s helpful to know the main R mailing lists (rhelp for example) and, often, to add ‘cran’, since it’s the main R repository. So while ‘R linear regression’ might help, you’ll get much better results searching for ‘rhelp linear regression’ or ‘cran linear regression’ (click the links to see the differences in search results).

CrossValidated (search google with ‘site:stats.stackexchange.com R linear regression’) has some great resources that are generally more focused on solving specific problems. The R Task Views are also incredibly helpful since they group packages by specific task, so there’s a spatial task view and a time series task view. You can add your suggestions for other resources in the comments.

Searching for Errors

Searching for solutions to errors is often one of the trickiest things. Generally you can’t put the whole error message in quotes because there are specific terms related to your code. For example, searching something like: “Error in is.data.frame(data) : object ‘peanutbutterpancakes’ not found” won’t turn up any good results because no one has had a problem with that variable name except you (although once this gets posted the search should return exactly one hit). Searching for the first part of the error, in quotes, should work, but you might need to add rhelp or cran if the error message is generic across a number of programming languages, and sometimes error messages are common across multiple functions, making the answer somewhat more difficult to find. Here, CrossValidated can be a very useful tool.

The Moral of the Story

One of the most important things to remember as you’re learning R is that we’ve all gone through this. R veterans are often able to immediately diagnose the problem that’s had you stumped for days, or the veteRan (see what I did there?) knows exactly what package and function you need at the drop of a hat. It can be intimidating, but you need to remember that they’ve probably encountered the same thing several times, and they’ve likely been just as stumped as you were. But now I’m letting you in on a big secret: (more than) half of troubleshooting R is learning how to use a search engine, the other half is just making the same mistake enough times that you stop making it.  Although maybe that’s just me. . .

Please add any tips or tricks you have for getting help in R below in the comments.

Published by

downwithtime

Assistant scientist in the Department of Geography at the University of Wisconsin, Madison. Studying paleoecology and the challenges of large data synthesis.

2 thoughts on “R tips I wish I had learned earlier – Getting Help in R”

  1. The recent sos package with its function FindFn() is also quite useful to find… well, functions. Also worth mentioning is the rdocumentation.org website, which has more search filter than help.search() or ?

  2. Another useful hint for starters: start debugging from the first line of code that throws up an error. This will often fix errors further down (for example if a declaration fails and the variable is never created). On that note, also make sure to fix all the errors even if a result is generated, as it could be using old (stored) versions of some variables and be giving spurious results.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s