Search code examples
rreactable

R - reactable colDef() across many columns, combined with another colDef() across many columns


I have multiple column definitions that are applied different sets of columns. How may I combine them?

Note: I cannot apply column definitions per column name, as these names change each day.

In this case I would like html applied to all columns, and a grey background applied to 3 columns.

library(reactable)
library(tidyverse)  
  
df <- structure(list(`Friday January 27` = c(NA, NA, NA, NA, NA, NA, 
                                             NA, NA, NA), 
                     `Saturday January 28` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA), 
                     `Sunday January 29` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA), 
                     `Monday January 30` = c("✔", "✔", "✔", "✔","✔", "✔", "✔", "✔", "✔"), 
                     `Tuesday January 31` = c("✔", "✔", "✔", "✔", "✔", "✔", "✔", "✔", "✔"), 
                     `Wednesday February 1` = c("❌","❌", "❌", "❌", "❌", "❌", "❌", "❌", "❌"), 
                     `Thursday February 2` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA)), 
                row.names = c("Route 1", "Route 2 Gardener", "Route 3", "Route 4", "Route 5 - Gardener", "Route 5 - Helper A", "Route 5 - Helper B", "Route 6", "Route 7"), 
                class = "data.frame")

columns_to_grey <- structure(c("Friday January 27", "Saturday January 28", "Sunday January 29", 
                               "Thursday February 2"), class = c("glue", "character"))

columns_to_html <- structure(c("Friday January 27", "Saturday January 28", "Sunday January 29", 
                               "Monday January 30", "Tuesday January 31", "Wednesday February 1", 
                               "Thursday February 2"), class = c("glue", "character"))
colDefs1 <- columns_to_grey |>
  set_names() |>
  map(~
        colDef(
          style = list(background = "#f7f7f7")
        )
  )

colDefs2 <- columns_to_html |>
  set_names() |>
  map(~
        colDef(
          html = TRUE
        )
  )

## How can I include colDefs2?
## Output not included due to reprex crashing R
# reactable(df, columns = colDefs1)

Created on 2023-02-01 with reprex v2.0.2


Solution

  • One ption would be to combine your two colDefs into one for which I use modifyList.

    Note: As I did not see any difference in the output whether I apply your colDefs2 to the reactable or not I slightly changed your data to check that both colDefs are applied, i.e. I added a cell with some blue colored text wrapped in a span tag.

    library(reactable)
    
    colDefs2[names(colDefs1)] <- lapply(
      names(colDefs1), 
      function(x) {
        modifyList(colDefs2[[x]], colDefs1[[x]])
      }
    )
    
    reactable(df, columns = colDefs2)
    

    enter image description here

    DATA

    df <- structure(
      list(
        `Friday January 27` = c(
          NA, NA, NA, NA, NA, NA,
          NA, NA, NA
        ),
        `Saturday January 28` = c(NA, NA, NA, NA, "<span style='color: blue'>BLUE</span>", NA, NA, NA, NA),
        `Sunday January 29` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA),
        `Monday January 30` = c("✔", "✔", "✔", "✔", "✔", "✔", "✔", "✔", "✔"),
        `Tuesday January 31` = c("✔", "✔", "✔", "✔", "✔", "✔", "✔", "✔", "✔"),
        `Wednesday February 1` = c("❌", "❌", "❌", "❌", "❌", "❌", "❌", "❌", "❌"),
        `Thursday February 2` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA)
      ),
      row.names = c("Route 1", "Route 2 Gardener", "Route 3", "Route 4", "Route 5 - Gardener", "Route 5 - Helper A", "Route 5 - Helper B", "Route 6", "Route 7"),
      class = "data.frame"
    )