Last active
January 18, 2023 21:06
-
-
Save nanxstats/a4d428f59c8b22267cd010424af34790 to your computer and use it in GitHub Desktop.
Add custom error messages to specific errors in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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