Search code examples
rerror-handlingwarnings

How to use special characters and colours in error messages in R?


I am writting a package in R, and I come around the warnings and error messages. In the book https://style.tidyverse.org/error-messages.html the use of ℹ and ✖ is adviced. However, it doesn't mention how to use the different special character.

On top of that, I would like to know how to colour different elements of the messages provided, such as when loading tidyverse.

Output with coloured parts

Thanks


Solution

  • The vctrs package is using rlang::format_error_bullets

    cat(rlang::format_error_bullets(c(i = "Info", x = "Bad")))
    #> ℹ Info
    #> x Bad
    

    This function is using cli::symbol$info and cli::symbol$cross which are ultimately just "ℹ" and "x"

    tidyverse:::tidyverse_attach uses crayon for colours.

    cat(paste0(crayon::bold("Attaching packages\n"), crayon::green("Green Text")))
    

    colours