rgt

How to have cells with rounded corners in {gt}?


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:


Solution

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

    enter image description here