R: Does the current R session support ANSI colors?

has_color {crayon} | R Documentation

## Does the current R session support ANSI colors?

### Description

Does the current R session support ANSI colors?

### Usage

    has_color()

### Details

The following algorithm is used to detect ANSI support:

- If the `crayon.enabled` option is set to `TRUE` with `options()`, then `TRUE` is returned. If it is set to something else than `TRUE` (typically `FALSE`), then `FALSE` is returned.

- Otherwise, if the standard output is not a terminal, then `FALSE` is returned.

- Otherwise, if the platform is Windows, `TRUE` is returned if running in ConEmu (https://conemu.github.io/) or cmder (http://cmder.net) with ANSI color support. Otherwise `FALSE` is returned.

- Otherwise, if the `COLORTERM` environment variable is set, `TRUE` is returned.

- Otherwise, if the `TERM` environment variable starts with `screen`, `xterm` or `vt100`, or matches `color`, `ansi`, `cygwin` or `linux` (with case insentive matching), then `TRUE` is returned.

- Otherwise `FALSE` is returned.

### Value

`TRUE` if the current R session supports color.

### Sinks

Note that `has_color()` returns `FALSE` if a sink is active (see `sink()`). It assumes that the constructed string will be printed to the standard output, and `sink()` redirects to a file, and usually you don't want ANSI colors in the file.

The same applies to the case when R's standard output is redirected to a file, from the command line, e.g.:

    R -q -e 'cat(crayon::red("no color here\n"))' > /tmp/crayon-test.txt
    cat /tmp/crayon-test.txt

### Examples

    has_color()

[Package crayon version 1.3.4 Index]