R/Debugging

From QERM Wiki
(Difference between revisions)
Jump to: navigation, search
(Profiling)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
==  Interactive Debugging in R ==
 
==  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 interactively debug your code. The purpose of these functions is to make the debugging process, which is critical in any programming assignment, as smooth as possible.  
+
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 <tt>
 
The most useful debugging functions are <tt>
Line 8: Line 8:
 
* trace
 
* trace
 
* options(error=recover)
 
* options(error=recover)
 +
</tt>
  
[[R-debug-tools.pdf | Here]] is a very helpful reference on interactive debugging by Roger Peng, UCLA.
+
{{Pdf|R-debug-tools.pdf|Here}} is a very helpful reference on interactive debugging by Roger Peng, UCLA.
 +
 
 +
== Profiling ==
 +
 
 +
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."'' [http://en.wikipedia.org/wiki/Optimization_(computer_science) by] Michael Jackson, and these [http://www.cs.cmu.edu/~jch/java/rules.html 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.  '''[http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/base/html/Rprof.html Rprof]''' is a function in R that lets you profile a function. 
 +
 
 +
Rprof()
 +
 +
mySlowFunction(1000)
 +
 +
Rprof(NULL)
 +
summaryRprof()
 +
 
 +
 
 +
===Ways to use Rprof's output===
 +
* <tt>summaryRprof()</tt> function
 +
* Perl script R CMD Rprof
 +
* [http://wiki.r-project.org/rwiki/doku.php?id=tips:misc:profiling Graphical visualization of Rprof output]
 +
* '''profr''' and '''proftools''' are packages for examining Rprof's output
 +
 
 +
 
 +
'''[http://www.statistik.uni-dortmund.de/useR-2008/tutorials/eddelbuettel.html High-Performance R]''': improving cpu time and memory use

Latest revision as of 22:57, 25 October 2011

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

  • traceback
  • debug
  • trace
  • options(error=recover)

Pdf10.png Here is a very helpful reference on interactive debugging by Roger Peng, UCLA.

Profiling

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


High-Performance R: improving cpu time and memory use

Personal tools