Debugging Tools in R Language

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 that 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 that led to the error). The traceback() function can be used which prints the list of functions that were called before the error occurred. However, this can be uninteresting if the error occurred at a top-level function.

Debugging Tools in R

Debugging Tools in R

The debugging tools in R are:

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. 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 the 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. It is especially useful if you need to track down an error that occurs in a base function.

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

The trace() function copies 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 a situation where a function must halt the execution.

SPSS Data Analysis

r faqs

How to View Source Code of R Method/ Function?

The article is about viewing the source code of R Method. There are different ways to view the source code of an R method or function. It will help to know how the function is working.

Source Code of R Method (Internal Functions)

If you want to see the source code of R method or the internal function (functions from base packages), just type the name of the function at the R prompt such as;

rowMeans
view R code of method

Functions or Methods from the S3 Class System

For S3 classes, the methods function can be used to list the methods for a particular generic function or class.

methods(predict)
Methods from the S3

Note that “Non-Visible functions are asterisked” means that the function is not exported from its package’s namespace.

One can still view its source code via the ::: function such as

stats:::predict.lm

or by using getAnywhere() function, such as

getAnywhere(predict.lm)

Note that the getAnywhere() function is useful as you don’t need to know from which package the function or method comes from.

Functions or Methods from the S4 Class System

The S4 system is a newer method dispatch system and is an alternative to the S3 system. The package ‘Matrix’ is an example of S4 function.

library(Matrix)
chol2inv
S4 Class System

The output already offers a lot of information. The standardGeneric is an indicator of an S4 function. The method to see defined S4 methods is to use showMethods(chol2inv), that is;

showMethods(chol2inv)
Source Code of R Method: view R code S4 System

The getMethod can be used to see the source code of one of the methods, such as,

getMethod ("chol2inv", "diagonalMatrix")
view R code S4 System

View Source Code of Unexported Functions

In the case of unexported functions such as ts.union, .cbindts, and .makeNamesTs from the stats namespace, one can view the source code of these unexported functions using the ::: operator or getAnywhere() function, for example;

stats::: .makeNamesTs
getAnywhere(.makeNamesTs)
view R code S4 System

https://itfeature.com

Online MCQs Test Preparation Website