Last active
November 29, 2021 21:23
-
-
Save HenrikBengtsson/85b89d0745455019af3a5668b47855a2 to your computer and use it in GitHub Desktop.
An attempt to automatically color annotate messages, warnings, and 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
if (getRversion() >= "4.0.0" && requireNamespace("crayon", quietly = TRUE) && | |
utils::packageVersion("startup") >= "0.16.0-9001") { | |
startup::on_session_start({ | |
globalCallingHandlers( | |
message = function(c) { | |
if (inherits(c, "tweaked_condition")) return() | |
c$message <- cli::col_blue(conditionMessage(c)) | |
class(c) <- c("tweaked_condition", class(c)) | |
message(c) | |
invokeRestart("muffleMessage") | |
}, | |
warning = function(c) { | |
if (inherits(c, "tweaked_condition")) return() | |
c$message <- cli::col_yellow(conditionMessage(c)) | |
class(c) <- c("tweaked_condition", class(c)) | |
warning(c) | |
invokeRestart("muffleWarning") | |
}, | |
error = function(c) { | |
if (inherits(c, "tweaked_condition")) return() | |
c$message <- cli::style_bold(cli::col_red(conditionMessage(c))) | |
class(c) <- c("tweaked_condition", class(c)) | |
stop(c) | |
} | |
) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment