glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
613 stars 79 forks source link

Incorrect cell color for grouped variable using groupBy in v0.3.0 #250

Closed fawda123 closed 1 year ago

fawda123 commented 1 year ago

With the latest update to v0.3.0, using the groupBy argument with a custom cell color function creates incorrect colors for the grouped headers. It looks like the color from the previous row is used.

library(reactable)
library(dplyr)

# color function for reactable cells
colfun <- function(x){

  case_when(
    x == 'a' ~ 'red', 
    x == 'b' ~ 'yellow', 
    x == 'c' ~ 'green' 
  )

}

totab <- tibble(
  var1 = c('a','a','b','b', 'c', 'c')
)

reactable(totab,  
          columns = list(
            var1 = colDef(
              style = function(x){
                list(background = colfun(x))
              })), 
          groupBy = 'var1'
        )

image

With the previous version 0.2.3, the grouped headers had no color.

image

- Session info -----------------------------------------------------------------------------------------------------------------
 setting  value
 version  R version 4.1.3 (2022-03-10)
 os       Windows 10 x64 (build 19043)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.1252
 ctype    English_United States.1252
 tz       America/New_York
 date     2022-07-06
 rstudio  2022.02.3+492 Prairie Trillium (desktop)
 pandoc   2.7.2 @ C:\\PROGRA~1\\Pandoc\\pandoc.exe

- Packages ---------------------------------------------------------------------------------------------------------------------
 package     * version date (UTC) lib source
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.0)
 cli           3.3.0   2022-04-25 [1] CRAN (R 4.1.3)
 crayon        1.5.1   2022-03-26 [1] CRAN (R 4.1.3)
 crosstalk     1.2.0   2021-11-04 [1] CRAN (R 4.1.2)
 DBI           1.1.3   2022-06-18 [1] CRAN (R 4.1.3)
 digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.2)
 dplyr       * 1.0.9   2022-04-28 [1] CRAN (R 4.1.3)
 ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
 fansi         1.0.3   2022-03-24 [1] CRAN (R 4.1.3)
 fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.0)
 generics      0.1.3   2022-07-05 [1] CRAN (R 4.1.3)
 glue          1.6.2   2022-02-24 [1] CRAN (R 4.1.3)
 htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.1)
 htmlwidgets   1.5.4   2021-09-08 [1] CRAN (R 4.1.1)
 jsonlite      1.8.0   2022-02-22 [1] CRAN (R 4.1.3)
 lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.1)
 magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.1.3)
 pillar        1.7.0   2022-02-01 [1] CRAN (R 4.1.2)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.1)
 reactable   * 0.3.0   2022-05-26 [1] CRAN (R 4.1.3)
 reactR        0.4.4   2021-02-22 [1] CRAN (R 4.1.0)
 rlang         1.0.3   2022-06-27 [1] CRAN (R 4.1.2)
 rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.2)
 tibble        3.1.7   2022-05-03 [1] CRAN (R 4.1.3)
 tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.1.3)
 utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
 vctrs         0.4.1   2022-04-13 [1] CRAN (R 4.1.3)
 yaml          2.3.5   2022-02-21 [1] CRAN (R 4.1.2)

 [1] C:/Users/Marcus.SCCWRP2K/R/win-library/4.1
 [2] C:/Program Files/R/R-4.1.3/library

----------------------------------------------
glin commented 1 year ago

Thanks for the bug report, it should be fixed in the v0.3.0.9000 development version (https://github.com/glin/reactable/commit/a396ccb6503ad3f30b4ea78933fe6325862d3518) now:

  • R style functions no longer apply to aggregated cells and rows incorrectly (@fawda123, #250).
  • JavaScript render functions and style functions no longer receive an invalid cellInfo.index or rowInfo.index property for aggregated cells and rows (#250).

The problem was that aggregated rows were being given their own row indices like 0 and 1, which doesn't make sense because they're not actual rows in the data. This was an oversight with the react-table v7 migration, and unfortunately doesn't have a good workaround besides staying on 0.2.3 or installing the development version.

fawda123 commented 1 year ago

Much appreciated @glin, thanks for all your work on this great package!