Three functions provide the the main entries into the debugging mode:
browser() - pauses the execution at a dedicated line in code (breakpoint)
debug()/undebug() - (un)sets a flag to run function in a debug mode (setting through)
debugonce() - triggers single stepping through a function
Breakpoints
calculate_median <-function(a) { a <-sort(a) n <-length(a) m <- (n +1) %/%2if (n %%2==1) { med <- a[m] } else {browser() med <-mean(a[m:m+1]) }return(med)}
## Example for running in RStudiov2 <-c(0, 1, 2, 2)calculate_median(v2)
Called from: calculate_median(v2)
debug: med <- mean(a[m:m + 1])
debug: return(med)
[1] 2
Debugger Commands
Command
Description
n(ext)
Execute next line of the current function
s(tep)
Execute next line, stepping inside the function (if present)
c(ontinue)
Continue execution, only stop when breakpoint in encountered
f(inish)
Finish execution of the current loop or function
Q(uit)
Quit from the debugger, executed program is aborted
debugonce()
debugonce() function allows to run and step through the function.
It is equivalent to setting a breakpoint at the first line of the function and then running it.