Skip to contents

Prints a data frame as an aligned console table with ANSI colors. By default each column receives one distinct color from palette; set by = "row" to use one color per row. Custom color_by specifications can still color individual values. Column widths are computed from visible ANSI text, so escape sequences and Chinese text do not break alignment in the console. Colors are emitted only when the output supports ANSI colors (a terminal or RStudio, or when options(thisplot.colors = TRUE) is set); captured output stays clean.

Usage

print_colored_table(
  x,
  color_by = NULL,
  colors = NULL,
  palette = "Chinese",
  by = "col",
  ...
)

Arguments

x

A data frame.

color_by

A character vector of column names to color, or a named list of color sources. If NULL (default), colors are assigned according to by.

colors

Optional color source used for all color_by columns unless overridden by a named list in color_by: a named hex vector whose names match the values of the colored columns, a plain hex vector recycled per row, or NA to fall back to palette.

palette

Palette name passed to palette_colors() for default row or column colors, or columns without explicit colors.

by

Whether default palette colors are assigned by "col" (default) or "row".

...

Arguments passed to other functions.

Value

x invisibly.

Examples

t1 <- data.frame(
  name = c("a", "b", "c"),
  col1 = c(1L, 2L, 3L),
  col2 = c(4L, 5L, 6L)
)

print_colored_table(t1)
#> name  col1  col2
#> a     1     4   
#> b     2     5   
#> c     3     6