Skip to content

Format any cell based on value  #703

@rjake

Description

@rjake

Just found gt and really like it. Using all the select helpers is 👌
The only thing I haven't found is a simple solution to format cells of any column based on the cell value. In my app, the number of columns will vary based on the user's selection. In this reprex, I want to bold any value <= 5.

Some of the solutions I've found weren't easy to implement:
This solution uses rlang + purrr
https://community.rstudio.com/t/highlighting-values-in-gt/80608/2

This one uses a for loop across each column:
https://stackoverflow.com/a/63945239/4650934

I was hoping to use something like:

tab_style(
  style = ...,
  locations = cells_body(
    columns = ...,
    condition = ~(.x <= 5))
  )
)

I hope you'll consider adding a helper function to make this easy to do. Thanks for all the hard work.

library(tidyverse)
library(gt)

set.seed(123)
matrix(sample(1:30, 30), 3, 10) %>% 
  as_tibble() %>% 
  gt()

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions