rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
587 stars 184 forks source link

`formatStyle()` doesn't work with Bootstrap 5 #1102

Closed alanfarahani closed 7 months ago

alanfarahani commented 7 months ago

Hi all,

The issue has already been raised in an SO post, but I wonder if a solution can be implemented in the back-end.

The problem

Unless I'm missing something, the bootstrap formatting of bslib appears to override any DT "formatStyle" calls for tables whose styles are not the default. That means choosing "auto" or "bootstrap5" will prevent any data-conditional table rendering apart from an implementation in css / javascript.

It would be great to use the bootstrap5 styling provided by bslib and programmatically format a table using "formatStyle".

MRE

library(shiny)
library(dplyr)
library(DT)
library(bslib)

ui <- bslib::page_sidebar(
  DT::DTOutput("tab1")
)

server <- function(input, output) {
  output$tab1 <- DT::renderDT({
    mtcars %>% 
      head() %>% 
      # this line works
      #DT::datatable(style = "default") %>% 
      DT::datatable() %>% 
      DT::formatStyle(
        columns = "mpg", 
        target = "row", 
        color = DT::styleEqual(c(21), "white"),
        backgroundColor = DT::styleEqual(c(21), "#4d4d4d"),
      )
  })
}

shinyApp(ui = ui, server = server)

Session Info


─ Session info ────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.0 (2023-04-21 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.utf8
 ctype    English_United States.utf8
 tz       America/Los_Angeles
 date     2023-12-06
 rstudio  2023.06.1+524 Mountain Hydrangea (desktop)
 pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 bsicons       0.1        2022-11-22 [1] CRAN (R 4.3.0)
 bslib       * 0.6.1.9000 2023-12-06 [1] Github (rstudio/bslib@148908a)
 cachem        1.0.8      2023-05-01 [1] CRAN (R 4.2.3)
 callr         3.7.3      2022-11-02 [1] CRAN (R 4.3.0)
 cli           3.6.1      2023-03-23 [1] CRAN (R 4.2.3)
 crayon        1.5.2      2022-09-29 [1] CRAN (R 4.3.0)
 crosstalk     1.2.1      2023-11-23 [1] CRAN (R 4.3.2)
 curl          5.0.1      2023-06-07 [1] CRAN (R 4.3.1)
 desc          1.4.2      2022-09-08 [1] CRAN (R 4.3.0)
 devtools      2.4.5      2022-10-11 [1] CRAN (R 4.3.0)
 digest        0.6.33     2023-07-07 [1] CRAN (R 4.3.2)
 dplyr       * 1.1.2      2023-04-20 [1] CRAN (R 4.2.3)
 DT          * 0.30       2023-10-05 [1] CRAN (R 4.3.2)
 ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.2.1)
 evaluate      0.23       2023-11-01 [1] CRAN (R 4.3.0)
 fansi         1.0.4      2023-01-22 [1] CRAN (R 4.2.3)
 fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.2.3)
 fs            1.6.3      2023-07-20 [1] CRAN (R 4.3.1)
 generics      0.1.3      2022-07-05 [1] CRAN (R 4.3.0)
 glue          1.6.2      2022-02-24 [1] CRAN (R 4.2.1)
 htmltools     0.5.7      2023-11-03 [1] CRAN (R 4.3.2)
 htmlwidgets   1.6.3      2023-11-22 [1] CRAN (R 4.3.2)
 httpuv        1.6.11     2023-05-11 [1] CRAN (R 4.2.3)
 jquerylib     0.1.4      2021-04-26 [1] CRAN (R 4.3.0)
 jsonlite      1.8.8      2023-12-04 [1] CRAN (R 4.3.2)
 knitr         1.45       2023-10-30 [1] CRAN (R 4.3.0)
 later         1.3.1      2023-05-02 [1] CRAN (R 4.2.3)
 lifecycle     1.0.4      2023-11-07 [1] CRAN (R 4.3.2)
 magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.2.1)
 memoise       2.0.1      2021-11-26 [1] CRAN (R 4.3.0)
 mime          0.12       2021-09-28 [1] CRAN (R 4.2.0)
 miniUI        0.1.1.1    2018-05-18 [1] CRAN (R 4.3.0)
 pillar        1.9.0      2023-03-22 [1] CRAN (R 4.3.0)
 pkgbuild      1.4.2      2023-06-26 [1] CRAN (R 4.3.1)
 pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.3.0)
 pkgload       1.3.2.1    2023-07-08 [1] CRAN (R 4.3.1)
 prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.3.0)
 processx      3.8.2      2023-06-30 [1] CRAN (R 4.3.1)
 profvis       0.3.8      2023-05-02 [1] CRAN (R 4.3.0)
 promises      1.2.0.1    2021-02-11 [1] CRAN (R 4.2.1)
 ps            1.7.5      2023-04-18 [1] CRAN (R 4.3.0)
 purrr         1.0.1      2023-01-10 [1] CRAN (R 4.2.3)
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.3.0)
 Rcpp          1.0.10     2023-01-22 [1] CRAN (R 4.2.3)
 remotes       2.4.2.1    2023-07-18 [1] CRAN (R 4.3.1)
 rlang         1.1.2      2023-11-04 [1] CRAN (R 4.3.2)
 rmarkdown     2.25       2023-09-18 [1] CRAN (R 4.3.0)
 rprojroot     2.0.3      2022-04-02 [1] CRAN (R 4.3.0)
 rsconnect     1.0.2      2023-08-17 [1] CRAN (R 4.3.1)
 rstudioapi    0.15.0     2023-07-07 [1] CRAN (R 4.3.1)
 sass          0.4.7      2023-07-15 [1] CRAN (R 4.3.2)
 sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.3.0)
 shiny       * 1.8.0      2023-11-17 [1] CRAN (R 4.3.0)
 stringi       1.8.2      2023-11-23 [1] CRAN (R 4.3.2)
 stringr       1.5.1      2023-11-14 [1] CRAN (R 4.3.0)
 tibble        3.2.1      2023-03-20 [1] CRAN (R 4.2.3)
 tidyselect    1.2.0      2022-10-10 [1] CRAN (R 4.3.0)
 urlchecker    1.0.1      2021-11-30 [1] CRAN (R 4.3.0)
 usethis       2.2.2      2023-07-06 [1] CRAN (R 4.3.1)
 utf8          1.2.3      2023-01-31 [1] CRAN (R 4.2.3)
 vctrs         0.6.4      2023-10-12 [1] CRAN (R 4.3.2)
 withr         2.5.0      2022-03-03 [1] CRAN (R 4.3.0)
 xfun          0.41       2023-11-01 [1] CRAN (R 4.3.2)
 xtable        1.8-4      2019-04-21 [1] CRAN (R 4.3.0)
 yaml          2.3.7      2023-01-23 [1] CRAN (R 4.2.3)
gadenbuie commented 7 months ago

Hi @alanfarahani and thanks for the excellent reprex! The root cause of the problem is that DT::formatStyle() works in a way that is incompatible with the way that tables are styled in Bootstrap 5. It isn't something bslib is doing specifically, just that the way that tables are styled has evolved and the styling assumptions made by DT no longer hold, at least in BS5.

I'm moved this issue to the DT repo. If a fix beyond using style = "default" is feasible, it will have to come from DT rather than bslib.

alanfarahani commented 7 months ago

Hi @gadenbuie, I had wondered whether this was possibly DT::formatStyle() issue, and now you've answered the question! Thanks for the detailed response, and for passing this along.

Edit: I should add, per the last issue posted to DT. That I have tried the reprex above using the development versions of both DT and bslib.

See session info below:


─ Session info ────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.0 (2023-04-21 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.utf8
 ctype    English_United States.utf8
 tz       America/Los_Angeles
 date     2023-12-07
 rstudio  2023.06.1+524 Mountain Hydrangea (desktop)
 pandoc   NA

─ Packages ────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 bsicons       0.1        2022-11-22 [1] CRAN (R 4.3.0)
 bslib       * 0.6.1.9000 2023-12-06 [1] Github (rstudio/bslib@148908a)
 cachem        1.0.8      2023-05-01 [1] CRAN (R 4.2.3)
 callr         3.7.3      2022-11-02 [1] CRAN (R 4.3.0)
 cli           3.6.1      2023-03-23 [1] CRAN (R 4.2.3)
 crayon        1.5.2      2022-09-29 [1] CRAN (R 4.3.0)
 crosstalk     1.2.1      2023-11-23 [1] CRAN (R 4.3.2)
 devtools      2.4.5      2022-10-11 [1] CRAN (R 4.3.0)
 digest        0.6.33     2023-07-07 [1] CRAN (R 4.3.2)
 dplyr       * 1.1.2      2023-04-20 [1] CRAN (R 4.2.3)
 DT          * 0.30.3     2023-12-07 [1] Github (rstudio/DT@b42dd91)
 ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.2.1)
 fansi         1.0.4      2023-01-22 [1] CRAN (R 4.2.3)
alanfarahani commented 7 months ago

This issue appears to be resolved in the development version.

Nissim74 commented 4 months ago

Hello everyone,

Sorry to comment in a closed post, but it appears I have the exact same problem, with the same reprex, with current CRAN versions and current development versions of bslib and DT, and the style="default" workaround is not an option in my case.

Is there anything I'm missing?

─ Session info ─────────────────────────────────────────────────────────────────────────────── setting value version R version 4.3.3 (2024-02-29 ucrt) os Windows 10 x64 (build 19045) system x86_64, mingw32 ui RStudio language (EN) collate English_United States.utf8 ctype English_United States.utf8 tz Europe/Paris date 2024-03-06 rstudio 2023.12.1+402 Ocean Storm (desktop) pandoc NA ─ Packages ─────────────────────────────────────────────────────────────────────────────── package * version date (UTC) lib source bslib * 0.6.1.9001 2024-03-06 [1] Github (rstudio/bslib@fb94eba) cachem 1.0.8 2023-05-01 [1] CRAN (R 4.3.3) callr 3.7.3 2022-11-02 [1] CRAN (R 4.3.2) cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.2) crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.2) crosstalk 1.2.1 2023-11-23 [1] CRAN (R 4.3.3) curl 5.2.1 2024-03-01 [1] CRAN (R 4.3.3) desc 1.4.3 2023-12-10 [1] CRAN (R 4.3.2) devtools * 2.4.5 2022-10-11 [1] CRAN (R 4.3.3) digest 0.6.34 2024-01-11 [1] CRAN (R 4.3.2) dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.2) DT * 0.32.1 2024-03-06 [1] Github (rstudio/DT@06d3ae6) ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.3.3) fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.3) fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.3) fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.2) generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.2) glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.2) htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.3.3) htmlwidgets 1.6.4 2023-12-06 [1] CRAN (R 4.3.3) httpuv 1.6.14 2024-01-26 [1] CRAN (R 4.3.3) jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.3.3) jsonlite 1.8.8 2023-12-04 [1] CRAN (R 4.3.2) later 1.3.2 2023-12-06 [1] CRAN (R 4.3.3) lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2) magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.2) memoise 2.0.1 2021-11-26 [1] CRAN (R 4.3.3) mime 0.12 2021-09-28 [1] CRAN (R 4.3.1) miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.3.3) pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.3) pkgbuild 1.4.3 2023-12-10 [1] CRAN (R 4.3.2) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.2) pkgload 1.3.4 2024-01-16 [1] CRAN (R 4.3.2) processx 3.8.3 2023-12-10 [1] CRAN (R 4.3.2) profvis 0.3.8 2023-05-02 [1] CRAN (R 4.3.3) promises 1.2.1 2023-08-10 [1] CRAN (R 4.3.3) ps 1.7.6 2024-01-18 [1] CRAN (R 4.3.2) purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.2) R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.2) Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.3) remotes 2.4.2.1 2023-07-18 [1] CRAN (R 4.3.2) rlang 1.1.2 2023-11-04 [1] CRAN (R 4.3.2) rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.3) sass 0.4.8 2023-12-06 [1] CRAN (R 4.3.3) sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.3) shiny * 1.8.0 2023-11-17 [1] CRAN (R 4.3.3) stringi 1.8.3 2023-12-11 [1] CRAN (R 4.3.2) stringr 1.5.1 2023-11-14 [1] CRAN (R 4.3.2) tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.3) tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.3) urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.3.3) usethis * 2.2.3 2024-02-19 [1] CRAN (R 4.3.3) utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.3) vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.2) withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.2) xfun 0.42 2024-02-08 [1] CRAN (R 4.3.2) xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.3) yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.2)
stla commented 3 months ago

See https://stackoverflow.com/a/78141878/1100107.