Interactive Debugging in R
Often times when we write our own functions, or use other functions in various packages, the error messages can sometimes get confusing. While good programming practice and patience are keys to solving these problems, R also has some other tools that can be used to make the debugging process, which is critical in any programming assignment, as engaging and smooth (and even fun!) as possible.
The most useful debugging functions are
Here is a very helpful reference on interactive debugging by Roger Peng, UCLA.
Keep in mind the 2 rules of performance optimization, "The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." by Michael Jackson, and these basic principles. If you are going to optimize your code, it's critical to profile it first, as our intuition as to what is slow is often wrong. Rprof is a function in R that lets you profile a function.
Rprof() mySlowFunction(1000) Rprof(NULL) summaryRprof()
Ways to use Rprof's output
- summaryRprof() function
- Perl script R CMD Rprof
- Graphical visualization of Rprof output
- profr and proftools are packages for examining Rprof's output
High-Performance R: improving cpu time and memory use