POP77001 Computer Programming for Social Scientists
Grace Murray Hopper popularised the term bug after in 1947 her team traced an error in the Mark II to a moth trapped in a relay.
Fixing a buggy program is a process of confirming, one by one, that the many things you believe to be true about the code actually are true. When you find that one of your assumptions is not true, you have found a clue to the location (if not the exact nature) of a bug.
Norman Matloff
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
Arthur Conan Doyle
print()print() statement can be used to check the internal state of a program during evaluation.ls() (and get()/mget()) to reveal all local objects.debug()/debugonce()).print(): Exampleprint() and ls(): Exampleprint(): Examplebrowser() - 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.| 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 |
Rather than waiting for a condition to occur, we can signal it ourselves.
stop()Rows: 44 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): dataset
dbl (2): x, y
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
try() for errorssuppressWarnings() for warningssuppressMessages() for messagestryCatch() define exiting handlerswithCallingHandlers() define calling handlerstryCatch(
error = function(cnd) {
# code to run when error is thrown
},
code_to_run_while_handlers_are_active
)
withCallingHandlers(
warning = function(cnd) {
# code to run when warning is signalled
},
message = function(cnd) {
# code to run when message is signalled
},
code_to_run_while_handlers_are_active
)
tryCatch() are called exiting handlers.== (or !=).TRUE or FALSE is expected is to use special functions:
all.equal() - approximately equalidentical() - exactly identical (incl. type)isTRUE() - whether value is TRUEisFALSE() - whether value is FALSEtestthat libraryExtra
── Error: The result is numeric ────────────────────────────────────────────────
Error in `calculate_median(c("a", "bc", "xyz"))`: is.numeric(a) is not TRUE
Backtrace:
▆
1. ├─testthat::expect_true(...)
2. │ └─testthat::quasi_label(enquo(object), label, arg = "object") at testthat/R/expect-constant.R:33:3
3. │ └─rlang::eval_bare(expr, quo_get_env(quo)) at testthat/R/quasi-label.R:45:3
4. └─global calculate_median(c("a", "bc", "xyz")) at rlang/R/eval.R:96:3
5. └─base::stopifnot(is.numeric(a))
Error:
! Test failed
All of old. Nothing else ever. Ever tried. Ever failed. No matter. Try again. Fail again. Fail better.
Samuel Beckett