Skip to contents

The `data_bars_pos_neg()` function is depreciated. The new version of `data_bars()` can handle both positive and negative values now. Please use `data_bars()` instead.

Usage

data_bars_pos_neg(data, colors = c("red", "green"), number_fmt = NULL)

Arguments

data

Dataset containing at least one numeric column.

colors

A minimum of two colors or a vector of colors. Colors should be given in order from negative values to positive values. Can use R's built-in colors or other color packages.

number_fmt

Optionally format numbers using formats from the scales package. Default is set to NULL.

Value

a function that applies positive and negative data bars to a column of numeric values.

Examples

data <- data.frame(
company = sprintf("Company%02d", 1:10),
profit_chg = c(0.2, 0.685, 0.917, 0.284, 0.105, -0.701, -0.528, -0.808, -0.957, -0.11))

## By default, the negative values are assigned a red bar,
## and the positive values are assigned a green bar
reactable(data,
bordered = TRUE,
columns = list(
 company = colDef(name = "Company",
 minWidth = 100),
 profit_chg = colDef(
   name = "Change in Profit",
   defaultSortOrder = "desc",
   align = "center",
   minWidth = 400,
   cell = data_bars(data))))
#> Error in x$width %||% settings$fig.width * settings$dpi: non-numeric argument to binary operator

## You can apply a relative color scale to the bars by assigning three or more colors
reactable(data,
bordered = TRUE,
columns = list(
  company = colDef(name = "Company",
  minWidth = 100),
  profit_chg = colDef(
  name = "Change in Profit",
  defaultSortOrder = "desc",
  align = "center",
  minWidth = 400,
  cell = data_bars(data,
  fill_color = c("#ff3030", "#ffffff", "#1e90ff")))))
#> Error in x$width %||% settings$fig.width * settings$dpi: non-numeric argument to binary operator