admin管理员组

文章数量:1336613

I need to make cells with rounded corners in {gt}, to make cells with color fill look like rounded rectangles. I know this is possible with {formattable}, and it should be possible with CSS border-radius: 5px;.

What I'm trying to achieve can be seen in these {formattable} examples:

I need to make cells with rounded corners in {gt}, to make cells with color fill look like rounded rectangles. I know this is possible with {formattable}, and it should be possible with CSS border-radius: 5px;.

What I'm trying to achieve can be seen in these {formattable} examples:

Share asked Nov 19, 2024 at 19:35 Bastián Olea HerreraBastián Olea Herrera 1,1008 silver badges24 bronze badges 1
  • 2 Here's a similar example: themockup.blog/static/resources/gt-cookbook-advanced#badges – Jon Spring Commented Nov 19, 2024 at 19:49
Add a comment  | 

1 Answer 1

Reset to default 2

This is kludgey, as I was having trouble combining the scaled (for shading determination) and raw (for display) values in one step. And I'd also prefer to map to a palette instead of manually calculating and HSL value. But it works as a demonstration of concept.

(Suggestions for improvement would be very welcome!)

add_badge <- function(x, y){
  div_out <- htmltools::div(
    style = paste0(
      "display: inline-block; padding: 2px 12px; border-radius: 5px;",
      "background: hsl(140, 50%, ",
      scales::percent(y, accuracy = 1),
      "); color: hsl(150, 10%, 50%);"
    ),
    x
  )
  
  as.character(div_out) |>
    gt::html()
}

mtcars |>
  head() |>
  dplyr::mutate(wt_scaled = scales::rescale(wt, to = c(0,1)),
                wt_shade  = purrr::map2(wt, wt_scaled, add_badge)) |>
  gt() 

本文标签: rHow to have cells with rounded corners in gtStack Overflow