kcuilla / reactablefmtr

Streamlined Table Styling and Formatting for Reactable
https://kcuilla.github.io/reactablefmtr/
Other
208 stars 27 forks source link

data_bars: text_position = "outside-base" leads to different horizontal alignment of bars #41

Open RoelVerbelen opened 2 years ago

RoelVerbelen commented 2 years ago

Thanks for making this useful extension package!

For readability, I'm trying to position the text on the left-hand side of the bars using text_position = "outside-base" but it's leading to a misalignment of the bars due differences in the width of the text. Even when using number_fmt = scales::label_percent(trim = FALSE) which produces strings of equal width, I can't align the bars.

library(dplyr)
library(reactablefmtr)

iris %>% 
  group_by(Species) %>%
  summarise(petal_length_low = mean(Petal.Length < 4)) %>% 
  reactable(
    columns = list(
      petal_length_low = colDef(
        cell = data_bars(., 
                         text_position = "outside-base", 
                         number_fmt = scales::label_percent(trim = FALSE))
      )
    )
  )

# Text descriptions have equal width
scales::label_percent(trim = FALSE)(c(1, 0.22, 0))
[1] "100%" " 22%" "  0%"

I'm surprised this is happening though, as I'm using the latest version from github (2.1.0) and the source code seems to try and deal with this situation via stringr::str_pad() (see here).

fegue commented 5 months ago

Yes, I have the same question and can't find an answer (2 years after it was asked). Weirdly enough, the first example on the Github Page that uses gradient colors (Cars, Price, Price vs Average) does have aligned bars in the column "Price".