-
Notifications
You must be signed in to change notification settings - Fork 0
/
_025_debugging.R
67 lines (55 loc) · 1.66 KB
/
_025_debugging.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Filename: _025_debugging.R
# Title: Debugging in R
# Author: Raghava | GitHub: @raghavtwenty
# Date Created: June 8, 2024 | Last Updated: June 8, 2024
# Language: R | Version: 4.4.0
# Set a breakpoint in a function
print("Setting a breakpoint in the 'mean' function")
debug(mean)
# Remove the breakpoint from a function
print("Removing the breakpoint from the 'mean' function")
undebug(mean)
# Enter the debugger manually
print("Entering the debugger manually")
browser()
# Print the call stack
print("Printing the call stack")
traceback()
# Debugging inside loops
print("Debugging inside loops using browser()")
for (i in 1:3) {
print(i)
if (i == 2) browser()
}
# Options for detailed error messages
print("Setting options for detailed error messages")
options(show.error.messages = TRUE, keep.source = TRUE)
# Trace function entry and exit
print("Tracing function entry and exit")
trace("mean", exit = quote(cat("Exiting mean\n")))
mean(1:10)
untrace("mean")
# Handling warnings as errors
print("Handling warnings as errors")
options(warn = 2)
# Example: intentionally cause a warning
sqrt(-1)
# Set conditional breakpoints
print("Setting a conditional breakpoint inside a function")
test_function <- function(x) {
if (x == 5) browser()
x + 1
}
print(test_function(3))
# Set the error option to use recover()
print("Setting the global error option to use recover()")
options(error = recover)
# Define a function that causes an error
print("Defining a function that will cause an error")
cause_error <- function() {
x <- log("a")
return(x)
}
# Call the function to trigger the error and enter recover mode
print("Calling the function to trigger an error")
cause_error()