Handling error in R can easily be done with debugger or by activating error options with recover.

options(error = recover)

Or to activate warning as error with:

options(warn = 2)

Nevertheless this blog post explain nicely other alternative including the code below that you can put in your .Rprofile.

options(error = function() {
  calls <- sys.calls()
  if (length(calls) >= 2L) {
    sink(stderr())
    on.exit(sink(NULL))
    cat("Backtrace:\n")
    calls <- rev(calls[-length(calls)])
    for (i in seq_along(calls)) {
      cat(i, ": ", deparse(calls[[i]], nlines = 1L), "\n", sep = "")
    }
  }
  if (!interactive()) {
    q(status = 1)
  }
})