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

Time:02-02

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 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"
)
  • Related