jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
195 stars 27 forks source link

gt_badge conditional on variables #123

Open aghaynes opened 7 months ago

aghaynes commented 7 months ago

Prework

Proposal

It would be nice to be able to set badges on only certain rows of a gt table. For instance, here's an example with missings

d <- tibble::tribble(
  ~a, ~b,
  "a", 1,
  "b", 2,
  "c", NA,
  "d", NA,
)

gt(d) |> 
  gt_badge(b,
           palette = c("red", "blue", "green")) |>
  sub_missing(missing_text = "")

image

There are two issues here. Firstly, the <br /> tag, which I guess might be a bug, and secondly, the badge itself on those <br /> tags, which would rather be blank instead.

It would be great to be able to do something like

gt(d) |> 
  gt_badge(b,
           palette = c("red", "blue"),
           rows = !is.na(b)) |>
  sub_missing(missing_text = "")
jthomasmock commented 2 months ago

I have this working locally now:

library(gtExtras)

d <- tibble::tribble(
  ~a, ~b,
  "a", 1,
  "b", 2,
  "c", NA,
  "d", NA,
)

d |> 
  #mutate(b = ifelse(is.na(b), "", b)) |> 
  gt() |> 
  sub_missing(missing_text = "") |> 
  gt_badge(b,
           palette = c("red", "blue"),
          rows = !is.na(b))

image