Background color for multirow cells in kableExtra

I'm trying to fill in the background color of a multirow cell with kableExtra, but like Goldilocks and the Three Bears, I can only get too little of the cell colored or too much (with color extending into the header). A reprex Rmd is below:

---
title: "Untitled"
output: pdf_document
---

```{r test, message = FALSE}
library("tidyverse")
library("kableExtra")

## Create data
dat <- tibble(
  x = 1:5,
  y = 6:10,
  summary = "foo"
)

## This fills in only the bottom cell of the collapsed cells
dat %>%
  mutate(summary = cell_spec(summary, "latex", background = "red")) %>%
  kable("latex", escape = FALSE) %>%
  add_header_above(c("new" = 2, " " = 1)) %>%
  collapse_rows(columns = 3)

## This half-fills the header with the background color
dat %>%
  kable("latex") %>%
  column_spec(3, background = "red") %>%
  add_header_above(c("new" = 2, " " = 1)) %>%
  collapse_rows(columns = 3)
```

The above yields the following results:

Is there a way I can get just the foo cell (fully) colored in?

One solution would be to add a row_spec to the header row

---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: true
---

```{r test, message = FALSE}
library("tidyverse")
library("kableExtra")

## Create data
dat <- tibble(
  x = 1:5,
  y = 6:10,
  summary = "foo"
)

dat %>%
  kable("latex") %>%
  column_spec(3, background = "red") %>%
  add_header_above(c("new" = 2, " " = 1)) %>%
  collapse_rows(columns = 3) %>% 
  row_spec(0, background = "white") # Color header row background 
```

multirow_color

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.