Skip to content

Instantly share code, notes, and snippets.

@Christoph999
Created March 31, 2016 12:09
Show Gist options
  • Save Christoph999/a3a06a256c4c502bbbca2eb72c75983c to your computer and use it in GitHub Desktop.
Save Christoph999/a3a06a256c4c502bbbca2eb72c75983c to your computer and use it in GitHub Desktop.
# Error on purpose in line 31-36. Testscript for learning debugging using different options.
imsbasics::clc()
f1 <- function(a) {
# message("Message f1")
b <- 10
return(a + 1)
}
f2 <- function(a) {
# message("Message f2")
c <- 100
return(a + 2)
}
f3 <- function(a) {
# message("Message f3")
d <- 100
return(a + 20)
}
f4 <- function(a) {
# browser() # ............................... If you know where the error occurs
# message("Message f4")
const <- "hurz"
d <- 1000
# a <- f3(a)
if (a < 0) {
a = const
} else {
a = const
}
res <- a + 3
return(res)
}
read.csv2 <- function(file, ...) { # From Hadley...
tryCatch(read.csv(file, ...), error = function(c) {
c$message <- paste0(c$message, " (in ", file, ")")
stop(c)
})
}
# read.csv("code/dummy.csv")
# read.csv2("code/dummy.csv")
old <- getOption("error")
# options(error = NULL)
# # used traceback to figure out where in the call stack an error occurred
# options(error = traceback)
# browse the environment in the current function call. Not possible to step back / up in hierarchy
options(error = browser)
# # use recover if you want to poke around in the environments for previous function calls.
# # In other words, you may want to \jump up to a higher level in the function call stack.
# options(error = recover)
# c <- f1(f4(f3(f2(f1(3))))) # ... Not continued in case of error. Thus, d unknown!
c <- try(f1(f4(f3(f2(f1(3)))))) # ... Continue in case of error. Thus, d = T!
# c <- tryCatch(f1(f4(f3(f2(f1(3))))), # ... Not continued in case of error. Thus, d unknown! But better error message!
# error = function(c) {
# c$message <- paste0(c$message, ": !!!your place for information!!!")
# stop(c)
# })
d <- T
options(error = old)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment