Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

color_scales not consistent in coloring NA #47

Open
ocelhay opened this issue Nov 22, 2022 · 0 comments
Open

color_scales not consistent in coloring NA #47

ocelhay opened this issue Nov 22, 2022 · 0 comments

Comments

@ocelhay
Copy link

ocelhay commented Nov 22, 2022

I would like all cells containing NA values to be with a white background but there seem to be a bug preventing it. (Full repex below).

With reactable v0.3.0 and reactablefmtr v2.0.0.
SCR-20221122-edu

SCR-20221122-ed5

library(reactable)
library(reactablefmtr)

data_reactable <- 
  structure(list(`Visit Status Aggregate` = c("Withdrawn", "Pending", 
  "Missed (unexplained)", "Missed (explained)", "Completed"), Baseline = c(NA, 
   NA, NA, NA, 11878L), `0.5 Year` = c(26L, NA, 4L, 462L, 11386L
   ), `1 Year` = c(69L, NA, 8L, 571L, 11230L), `1.5 Year` = c(89L, 
  NA, 4L, 694L, 11091L), `2 Year` = c(127L, NA, 11L, 750L, 10990L
  ), `2.5 Year` = c(141L, NA, 29L, 1376L, 10332L), `3 Year` = c(169L, 
NA, 63L, 1160L, 10486L), `3.5 Year` = c(191L, NA, 171L, 1800L, 
9716L), `4 Year` = c(241L, 1427L, 142L, 1080L, 8988L), `4.5 Year` = c(247L, 
  3095L, 1223L, 1432L, 5881L), `5 Year` = c(258L, 7214L, 279L, 
248L, 3879L), `5.5 Year` = c(258L, 9568L, 326L, 170L, 1556L), 
 `6 Year` = c(258L, 11517L, NA, 1L, 102L)), row.names = c(NA, 
  -5L), class = c("tbl_df", "tbl", "data.frame"))


 def_column <- function(data) {
  columns <- setdiff(names(data), "Visit Status Aggregate") 
  c(
    rep(
      x = list(
        xaxis = colDef(
          align = "center",
          cell = function(value, index, name) {
            denom <- (data |> dplyr::pull(name) |> sum(na.rm = TRUE))
            if (is.na(value)) return("-")
            return(glue::glue("{format(value, big.mark = ',')} </br><small>{scales::label_percent(accuracy = 0.1)(value / denom)}</small>"))
          }
        )
      ),
      times = length(columns)
    ) |> 
      setNames(columns)
  )
}


reactable(
  data_reactable, 
  outlined = TRUE,
  pagination = FALSE,
  defaultColDef = colDef(
    style = color_scales(
      data_reactable,
      colors = RColorBrewer::brewer.pal(9, "Blues")[3:9]
    ),
    html = TRUE
  ),
  columns = def_column(
    data = data_reactable
  )
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant