You will get stuck while learning to code.
R has lots of resources for problem-shooting. You should familiarize yourself with the following:
When I was younger, so much younger than today
I never needed anybody’s help in any way
But now these days are gone, I’m not so self assured
Now I find I’ve changed my mind and opened up the doors
Your code is going to fail. A lot. Bugs and broken dreams code are a normal part of any programming task. They never go away, but you get better at diagnosing and fixed them!
The following is a brief taxonomy of errors in R, and some strategies to overcome them.
Two simple criteria must be met in order to recognize trouble with your code (Peng et al. 2017):
Oftentimes, the best case scenario is that R will throw an error message. Error messages signal that R has encountered some fatal flaw in your code and, in consequence, nothing happens.
The next best scenario is that R will throw a warning message. Warning messages signal that R has successfully evaluated and executed code, but something unexpected happened, and R wants you to know about it.
The worst and most pernicious scenario is when your code runs perfectly fine, without error or warning messages, yet the output isn’t exactly what you’d wanted. If you recognize this, that’s great. If you don’t recognize this, errors flow downstream and can be difficult to detect and fix. Always inspect your output.
Oftentimes, users place too much emphasis on fixing their expected results and fail to consider their original expectations. Never neglect revisiting your expectations, as it’s possible that they, not your code, may explain an error (Peng et al. 2017).
When you’re in a jam, the foremost strategy for resolving said jam is to read any relevant documentation.
All base R functions, as well as packages, functions, and datasets downloaded from CRAN (Comprehensive R Archie Network) contain documentation that can be accessed via the help()
or ?
functions.
When called, the Help pane displays built-in documentation, which typically contains names, descriptions, templates, arguments, values, references, related objects, and examples.
Observe the following calls to help()
on function median()
:
Datasets found “in the wild” often have documentation containing metadata, variable definitions, and other helpful information, while many functions have external documentation outside of CRAN.
Documentation for the Pheonix Fire Department Data
“A vignette is like a book chapter or an academic paper: It can describe the problem that your package is designed to solve, and then show the reader how to solve it.” (Hadley Wickham)
Some R packages have vignettes, or more comprehensive and thorough overviews than typical documentation. Vignettes are written for the express purpose of learning how to use a package. While vignettes are easy to find in a conventional search engine, you can open a vignette from within R by calling function browseVignettes()
and a quotes package name. For example:
Help me if you can, I’m feeling down
And I do appreciate you being round
Help me get my feet back on the ground
Won’t you please, please help me
While documentation tends to require a bit more reading, there’s a good chance that someone has run into the same problem you’ve encounters. The internet has a long memory and the following strategies show how to exploit it.
You can take advantage of error and warning messages by copying them from your console and pasting them directly into an online search engine, verbatim and quoted. It often helps to include the function or package name outside of the quoted console message.
Quoting Error Messages in Search Engines
One of the best troubleshooting strategies is to search for answers in online forums like Stack Overflow or Cross Validated. Archives like the “R-help” mailing list are also searchable.
Potential Solutions as Seen on Stack Overflow
Help, I need somebody
Help, not just anybody
Help, you know I need someone, help
Asking for help from another human should always be your last recourse after exhausted all of the above strategies. That said, if you’re still in a jam, there are several sources for human help:
Importantly, there are established best practices in asking R-related questions. Make sure to:
Pro Tip: While not the best for specific coding problems, consider “tweeting” a question that includes the hashtag “#rstats”. Followers of #rstats often respond quickly, accurately, and sometimes in great numbers.
Whether your poring over documentation, performing web, forum, and archive searches, or asking another human, language precision is key to troubleshooting. Hence, building your R vocabulary is critical to learning how to learn R. For example, instead of searching: “How do I combine two tables that share a column?”, search “join data frames on key variable r”, or something similar. The latter term, with precise R-specific jargon, is much more quick to fructify.
The following resources may prove valuable for both learning how to learn R and troubleshooting:
I) Full-Length Text
II) Documentation & Vignettes
III) Forums, Email Lists, & Communities
IV) Asking R-Related Questions
V) More ASCII Dragons