What is debug() in R?

The debug() method in R allows the user to debug a function, line by line, upon its execution.

Parameters

The name of the function that needs to be debugged is the parameter in debug().

Usage

Right before the function call, the debug() method is used. On the execution of the program, the debug() method continues to print the values of the variables in the function, line by line. It can also produce graphs of the results of the code.

There are a few actions that can be done once the debug() method, as listed below.

  • Type ‘c’ to make the debug() function continue till the end of the code section
  • Type ‘Q’ to quit the debugging
  • Type ‘where’ to show the point where the user is in the call stack of the function

Code

Here, the debug() method calls the fun() function defined by the user. It tests the execution on the assigned parameters of fun() and runs the code each line at a time.

fun <- function(value1,value2){
subval <- value2 - value1
square <- subval^2
get <- sum(square)
get
}
set.seed(100)
value2 <- rnorm(100)
debug(fun)
fun(1,value2)