Detect the number of ANSI colors to use
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")
stream |
The stream that will be used for output, an R connection
object. It can also be a string, one of |
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:
If the cli.num_colors
options is set, that is returned.
If the R_CLI_NUM_COLORS
env var is set to a non-empty value,
then it is used.
If the crayon.enabled
option is set to FALSE
, 1L is returned.
(This is for compatibility with code that uses the crayon package.)
If the crayon.enabled
option is set to TRUE
and the
crayon.colors
option is not set, then 8L is returned.
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.)
If the NO_COLOR
environment variable is set, then 1L is returned.
If we are in knitr, then 1L is returned, to turn off colors in
.Rmd
chunks.
If stream
is stderr()
and there is an active sink for it, then
1L is returned.
If R is running inside RGui on Windows, or R.app on macOS, then we return 1L.
If R is running inside RStudio, with color support, then the appropriate number of colors is returned, usually 256L.
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.)
If stream
is not a terminal, then 1L is returned.
If R is running on Unix, inside an Emacs version that is recent enough to support ANSI colors, then 8L is returned.
If stream
is not the standard output or error, then 1L is returned.
If we are on Windows, under ComEmu or cmder, or ANSICON is loaded, then 8L is returned.
Otherwise if we are on Windows, return 1L.
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.
If tput colors
fails, we try to guess. If COLORTERM
is set
to any value, we return 8L.
If TERM
is set to dumb
, we return 1L.
If TERM
starts with screen
, xterm
, or vt100
, we return 8L.
If TERM
contains color
, ansi
, cygwin
or linux
, we return 8L.
Otherwise we return 1L.
Integer, the number of ANSI colors the current R session
supports for stream
.
num_ansi_colors()
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.