debug() Function
The debug() function is used to set a debugging mode for a function. When a function is called in debugging mode, execution pauses at the start of the function, allowing you to step through it line by line.
How to Use debug()
Setting Up Debugging Mode
Use debug() to initiate debugging for a specific function.
# Define a function to debug my_function <- function(x) { y <- x + 1 z <- y * 2 return(z) } # Set debug mode for the function debug(my_function)
Calling the Function
When you call the function, R will enter debugging mode, stopping at the beginning of the function.
# Call the function; execution will pause result <- my_function(5)
Single-Stepping Through Code
In debugging mode, you can use the following commands to control execution:
-
- n (Next): Execute the next line of code.
- s (Step): Step into the function if the current line calls another function.
- c (Continue): Continue execution until the function returns.
- Q (Quit): Exit debugging mode.
Example:
# Start debugging mode debug(my_function) # Execute the function result <- my_function(5)
When execution pauses, you can type n to proceed to the next line or s to step into any function calls.
Stopping Debugging Mode
To stop debugging a function, use undebug().
# Stop debugging mode undebug(my_function)
browser() Function
The browser() function allows you to insert a debugging point anywhere in your code. When R encounters browser(), execution pauses, and you can interactively explore the environment.
How to Use browser()
Inserting browser() in Your Code
Add browser() to the code where you want to start debugging.
# Define a function with browser() my_function <- function(x) { y <- x + 1 browser() # Execution will pause here z <- y * 2 return(z) }
Calling the Function
When the function is called, execution will pause at the browser() statement.
# Call the function; execution will pause at browser() result <- my_function(5)
Interacting in Browser Mode
While in browser mode, you can use several commands:
-
- n (Next): Proceed to the next line within the same function.
- s (Step): Step into any function calls on the current line.
- c (Continue): Continue execution until the end of the function or until the next browser() call.
- Q (Quit): Exit browser mode and stop execution.
Example:
# Call the function result <- my_function(5) # Execution pauses at browser()
In the browser mode, type n to step to the next line or s to step into any function calls. You can also inspect variables directly in the console.
Removing browser() Statements
Once you’ve finished debugging, remove the browser() statements from your code.
# Remove the browser() statement from your function my_function <- function(x) { y <- x + 1 z <- y * 2 return(z) }
Example Walkthrough
Here’s a complete example that demonstrates both debug() and browser().
Example Code:
# Define a function with a bug example_function <- function(a, b) { c <- a + b browser() # Insert browser for debugging d <- c / 2 return(d) } # Use the debug function debug(example_function) # Call the function to trigger debugging result <- example_function(10, 5) # Output result (this will not be reached until browser() is handled) print(result)
Using browser() in this example:
- Call example_function(10, 5): Execution will pause at browser().
- In Browser Mode:
- Type n to proceed to d <- c / 2.
- Inspect variable values (a, b, c, d) by typing their names.
- Type c to continue execution until the end of the function.
Using debug() in this example:
- Call example_function(10, 5): Execution will pause at the start of example_function.
- In Debug Mode:
- Type n to step through each line.
- Type s to step into any nested function calls (if there were any).
- Type c to continue execution.
- Remove Debugging Tools:
# Remove debugging tools undebug(example_function
Conclusion
Using the debug() and browser() functions in R allows for precise control over code execution, enabling detailed examination and troubleshooting of issues. debug() is useful for pausing at the start of a function and stepping through it, while browser() allows you to insert interactive breakpoints anywhere in your code. Together, these tools enhance your ability to diagnose and fix problems efficiently.