r - How to have cells with rounded corners in {gt}? - Stack Overflow

I need to make cells with rounded corners in {gt}, to make cells with color fill look like rounded rect

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() 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742402880a4437263.html

相关推荐

  • r - How to have cells with rounded corners in {gt}? - Stack Overflow

    I need to make cells with rounded corners in {gt}, to make cells with color fill look like rounded rect

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信