rr-markdownkablekableextra

How to remove the name of first column of a table with collapse rows with rmarkdown and kableExtra


I'm doing a table with a combination of collapsed and packed rows. I want to remove the name the firstof the column, but when I do so kable removes the collapsing.

Any ideas?

Here an reproducible example code with the same errror:

library(tidyverse)
library(kableExtra)

data("iris") 

Tbl <- iris %>% 
  pivot_longer(cols = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)) %>% 
  separate(name, sep = 5, into = c("Flower_part", "Measure")) %>% 
  group_by(Species, Flower_part, Measure) %>% 
  summarise(Mean = mean(value),
            SD = sd(value)) 

#Table with colapsed rows and the name of column
Tbl %>% 
  ungroup() %>% 
  select(-Species) %>% 
  kbl(digits = 3) %>%
  kable_classic("striped", full_width = T) %>% 
  collapse_rows(columns = 1, valign = "top") %>% 
  pack_rows(index = table(Tbl$Species), indent = FALSE)

#Table with colapsed rows and the name of column

#Table with a "blank" name with the collapsing not working.
Tbl %>% 
  ungroup() %>% 
  select(-Species) %>% 
  rename(" " = "Flower_part") %>%
  kbl(digits = 3) %>%
  kable_classic("striped", full_width = T) %>% 
  collapse_rows(columns = 1, valign = "top") %>% 
  pack_rows(index = table(Tbl$Species), indent = FALSE)

#Table with a "blank" name with the collapsing not working.


Solution

  • You can use unicode:

    Tbl %>% 
      ungroup() %>% 
      select(-Species) %>% 
      rename("\U00A0" = "Flower_part") %>%
      kbl(digits = 3) %>%
      kable_classic("striped", full_width = T) %>% 
      collapse_rows(columns = 1, valign = "top") %>% 
      pack_rows(index = table(Tbl$Species), indent = FALSE)
    

    enter image description here