Certain Unix and Windows terminals, and also certain R GUIs, e.g. RStudio, support styling terminal output using special control sequences (ANSI sequences).

num_ansi_colors() detects if the current R session supports ANSI sequences, and if it does how many colors are supported.

num_ansi_colors(stream = "auto")

Arguments

stream The stream that will be used for output, an R connection object. It can also be a string, one of "auto", "message", "stdout", "stderr". "auto" will select stdout() if the session is interactive and there are no sinks, otherwise it will select stderr().

Value

Integer, the number of ANSI colors the current R session supports for stream.

Details

The detection mechanism is quite involved and it is designed to work out of the box on most systems. If it does not work on your system, please report a bug. Setting options and environment variables to turn on ANSI support is error prone, because they are inherited in other environments, e.g. knitr, that might not have ANSI support.

If you want to turn off ANSI colors, set the NO_COLOR environment variable to a non-empty value.

The exact detection mechanism is as follows:

1. If the cli.num_colors options is set, that is returned.

2. If the R_CLI_NUM_COLORS env var is set to a non-empty value, then it is used.

3. If the crayon.enabled option is set to FALSE, 1L is returned. (This is for compatibility with code that uses the crayon package.)

4. If the crayon.enabled option is set to TRUE and the crayon.colors option is not set, then 8L is returned.

5. If the crayon.enabled option is set to TRUE and the crayon.colors option is also set, then the latter is returned. (This is for compatibility with code that uses the crayon package.)

6. If the NO_COLOR environment variable is set, then 1L is returned.

7. If we are in knitr, then 1L is returned, to turn off colors in .Rmd chunks.

8. If stream is stderr() and there is an active sink for it, then 1L is returned.

9. If R is running inside RGui on Windows, or R.app on macOS, then we return 1L.

10. If R is running inside RStudio, with color support, then the appropriate number of colors is returned, usually 256L.

11. If R is running on Windows, inside an Emacs version that is recent enough to support ANSI colors, then 8L is returned. (On Windows, Emacs has isatty(stdout()) == FALSE, so we need to check for this here before dealing with terminals.)

12. If stream is not a terminal, then 1L is returned.

13. If R is running on Unix, inside an Emacs version that is recent enough to support ANSI colors, then 8L is returned.

14. If stream is not the standard output or error, then 1L is returned.

15. If we are on Windows, under ComEmu or cmder, or ANSICON is loaded, then 8L is returned.

16. Otherwise if we are on Windows, return 1L.

17. Otherwise we are on Unix and try to run tput colors to determine the number of colors. If this succeeds, we return its return value, except if the TERM environment variable is xterm and tput returned 8L, we return 256L, because xterm compatible terminals tend to support 256 colors.

18. If tput colors fails, we try to guess. If COLORTERM is set to any value, we return 8L.

19. If TERM is set to dumb, we return 1L.

20. If TERM starts with screen, xterm, or vt100, we return 8L.

21. If TERM contains color, ansi, cygwin or linux, we return 8L.

22. Otherwise we return 1L.

Other ANSI styling: ansi-styles, combine_ansi_styles(), make_ansi_style()
num_ansi_colors()
#> [1] 256