Debugging Tools in R Language

The built-in debugging tools (programs: functions) in R statistical computing environment are traceback(), debu(), browser(), trace(), and recover(). The purpose of the debugging tools is to help the programmer find unforeseen (غیر متوقع، جس کی اُمید نہ ہو) problems quickly and efficiently.

The R system has two main ways of reporting a problem in executing a function. One of them is a warning message while the other one is a simple error. The purpose of the warning is to tell the user (programmer) that “something unusual happened during the execution of the function, but the function was nevertheless able to execute to completion”. Writing a robust code (code which checks for imputing errors) is important for larger programs.

> log(-1)     #produce a warning (NaN)
> message <- function(x){
            if(x > 0)
            print(“Hello”)
            else
            print(“Goodbye”)
   }

The log(-1) will result in a fatal error, not a warning. The first thing one should do is to print the call stack (print the sequence of function calls which led to the error). The traceback() function can be used which prints the list of functions which were called before the error occurred. However, this can be uninteresting if the error occurred at a top-level function.

The traceback() Function

The traceback() function prints the sequence of function calls in reverse order from the top.

The debug() Function

The debug() function takes a single argument (the name of a function) and steps through the function line-by-line to identify the specific location of a bug, that function is flagged for debugging. In order to unflag a function, undebug() function is used. A function flagged for debugging does not execute in a usual way, rather, each statement in the function is executed one at a time and the user can control when each statement gets executed. After a statement is executed, the function suspends and the user is free to interact with the environment.

The browser() Function

The browser() function can be used to suspend execution of a function so that the user can browse the local environment.

The trace() Function

The trace() function is very useful for making minor modifications to function “on the fly” without having to modify functions and re-sourcing them. If is especially useful if you need to track down an error which occurs in a base function.

> trace(“mean”, quote( if( any(is.nan(x) ) ){ browser() }), print = FALSE)

The trace() function copy the original function code into a temporary location and replaces the original function with a new function containing the insert code.

The recover() Function

The recover() function can help to “jump up” to a higher level in the function call stack.

> options(error=recover)

The error option tells R what to do in the situation where a function must halt the execution.

Debugging Tools in R Language

Leave a Reply

Scroll to top
x  Powerful Protection for WordPress, from Shield Security
This Site Is Protected By
Shield Security