Search code examples
rreactable

Format groupby rows in reactable


In reactable, is there a way to style the entire row of groupBy header? e.g. to add a border to the bottom like this:

enter image description here

Code for table:

library(reactable)

data <- MASS::Cars93[10:22, c("Manufacturer", "Model", "Type", "Price", "MPG.city")]

reactable(
  data,
  groupBy = "Manufacturer",
  defaultExpanded = TRUE
)

Solution

  • I was able to figure this out with the help of Copilot. You can use a JavaScript function that checks if the row has any sub rows, and if it does then it adds a border:

    library(reactable)
    
    data <- MASS::Cars93[10:22, c("Manufacturer", "Model", "Type", "Price", "MPG.city")]
    
    reactable(
      data,
      groupBy = "Manufacturer",
      defaultExpanded = TRUE,
      rowStyle = JS("function(rowInfo) {
        if (rowInfo.subRows.length) {
          return { borderBottom: '3px solid #800080' }
        }
      }"),
    )