Skip to content

Instantly share code, notes, and snippets.

@nanxstats
Last active January 18, 2023 21:06
Show Gist options
  • Save nanxstats/a4d428f59c8b22267cd010424af34790 to your computer and use it in GitHub Desktop.
Save nanxstats/a4d428f59c8b22267cd010424af34790 to your computer and use it in GitHub Desktop.
Add custom error messages to specific errors in R
# Add custom error messages to specific errors in R
error_handler <- function() {
rlang::entrace()
e <- rlang::last_error()$message
if (grepl("there is no package called", x = e)) {
message("* This can be a custom error message with multiple lines.")
}
}
if ("rlang" %in% rownames(installed.packages())) {
options(error = error_handler)
options(rlang_backtrace_on_error = "none")
}
# Targeted errors
library("not.exist")
# Other errors
1 + "1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment