Learn R Programming

reactablefmtr (version 0.1.0)

data_bars: Add horizontal bars to rows in a column

Description

The `data_bars()` function conditionally adds a horizontal bar to each row of a column. The length of the bars are relative to the value of the row in relation to other values within the same column. It should be placed within the cell argument in reactable::colDef.

Usage

data_bars(data, colors = "#1e90ff", background = "white", commas = NULL)

Arguments

data

Dataset containing at least one numeric column.

colors

A single color or a vector of colors. Colors should be given in order from low values to high values. Can use R's built-in colors or other color packages.

background

Optionally assign a color to use as the background for cells. Default is set to white.

commas

Optionally format values as commas. Default is set to NULL or FALSE.

Value

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

Examples

Run this code
# NOT RUN {
library(reactable)
data <- MASS::Cars93[20:49, c("Make", "MPG.city", "MPG.highway")]

## Horizontal bars with lengths relative to cell value
reactable(data,
columns = list(
MPG.city = colDef(
name = "MPG (city)",
align = "left",
cell = data_bars(data, "dodgerblue"))))

## Add background color
reactable(data,
columns = list(
MPG.city = colDef(
name = "MPG (city)",
align = "left",
cell = data_bars(data, "dodgerblue", "grey"))))

## Conditionally color data bars based on their relative values
## by supplying more than one color
## and apply across all numeric columns using reactable::defaultColDef
reactable(data,
pagination = FALSE,
defaultSortOrder = "desc",
defaultSorted = "MPG.city",
defaultColDef = colDef(
cell = data_bars(data,
colors = c("firebrick1","gold","limegreen"))))

# }

Run the code above in your browser using DataLab