Sunday, December 23, 2012

R DEBUGGING

R DEBUGGING



REVISED: Saturday, March 2, 2013



Debugging R

"Object not found" errors, syntax errors, and all the rest are the three types of errors.

I.  R ERROR INDICATORS

A. MESSAGE

A generic notification or diagnostic message produced by the message( ) function; execution of the function continues.

B. WARNING

A warning indicates something is wrong but not fatal; execution of the function continues; generated by the warning( ) function.

C. ERROR

An error indicates an execution stopping fatal problem has occurred.  Errors are generated by the stop( ) function.

D. CONDITION
Programmers can create their own conditions, generic concepts indicating that something unexpected can occur.

II. FUNCTION ERRORS

How do you know something is wrong with your function?

A. DEBUGGING QUESTIONS

1. What was your input? How did you call the function?

2. What were you expecting; were you expecting output, message, other results?

3. What did you get?

4. How does what you get differ from what you were expecting?

5. Were you expecting correct in the first place?

6. Can you reproduce the problem exactly?

III. R DEBUGGING TOOLS

Most debugging takes place either through calls to browser( ) or debug( ).

A. traceback( ) 


traceback( ) prints out the function call stack after an error occurs, does nothing if there is no error. traceback( ) is one of your most important debugging tools, take the time to learn how to use it.

> str(traceback)
function (x = NULL, max.lines = getOption("deparse.max.lines"))
>

B. debug( ) 


debug( ) flags a user defined function for "debug mode" allowing you to step through your user defined function one line at a time.

> str(debug)
function (fun, text = "", condition = NULL)
>
 


You can single step through your R code line by line using debug( ) or browser( ).

C. browser( ) 


browser( ) suspends the operation of a user defined function wherever it is called and puts the user defined function into debug mode.

> str(browser)
function (text = "", condition = NULL, expr = TRUE, skipCalls = 0L)
>

Use the command q to exit the browser( ) mode.

D. trace( ) 

trace( ) allows you to insert debugging code into a function at specific places.

> str(trace)
function (what, tracer, exit, at, print, signature, where = topenv(parent.frame( )), edit = FALSE)
>

trace( ) could be used with the lm( ) linear modeling function.

> str(lm)
function (formula, data, subset, weights, na.action, method = "qr", 
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 

contrasts = NULL, offset, ...)  
>

E. recover( ) 

recover( ) allows you to modify the error behavior so that you can browse the error call stack.

> str(recover)
function ( )  
>

These are interactive tools designed to help you pick through a function. You can also insert print( ) or cat( ) statements in the function.

IV. COMMON ERRORS

A. SYNTAX ERRORS

Wrong type of closing brace, missing commas, and unmatched parentheses are common syntax mistakes .

B. OBJECT NOT FOUND

Errors of the "object not found" variety can have one of several causes. For example, the name is not spelled correctly; the capitalization is wrong; the package or file containing the object is not on the search list.

C. MISMATCHED OPERATORS

Trying to use operators intended for numeric values, on character values.

V. REFERENCES

The Art of R Programming, A Tour of Statistical Design by Norman Matloff (2009). 

Enjoy debugging R!


Elcric Otto Circle







-->




-->




-->

















How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"

No comments:

Post a Comment