Cell merge by column in kableExtra

Hi folks,

kableExtra::collapse_rows provides a nice way to create tables with collapsed (or merged) cells by row. I was wondering if kableExtra also provides a method to collapse cells by column. For instance, in the example below, I would like to merge the b's in the 2nd row of my table.

If this functionality is not available in kableExtra, are you aware of a package that provides the ability to:

  • collapse cells by column and/or row, and
  • add column headers on grouped columns
    ?

Thanks in adance

require(knitr)
require(kableExtra)

dt <- data.frame(
  var1 = c(1, 2, 3, 3, 4),
  var2 = c('a', 'b', 'c', 'c', 'e'),
  var3 = c('f', 'b', 'g', 'h', 'i')
)

kable(dt, align = "c") %>%
  kable_styling(full_width = FALSE) %>%
  collapse_rows(columns = 1:3, valign = "middle")
```
1 Like

Just wanna chime in and say that I am also looking for the possibility to merge rows in kable tables. I tried looking at the code for the collapse_rows function to try and adapt it to columns. I suspect that there should be a way of inserting the colspan HTML argument instead of rowspan, which the collapse_row function uses. However, I am not proficient enough in HTML nor in the nitty gritty details of function creation in R to actually modify the original function to suit my needs.

Hi @mafw

I found that the flextable package provides functionality to merge cells by row and/ column. However, it seems to lack latex reporting functionality, which I need. Anyways, depending on your requirements, flextable might be an option for you.

1 Like

Thanks for the advice! I'll look into the flextable package. Maybe I can try and adapt the row merge function from that package to my kableextra workflow...

@mafw

The more I look into the capabilities of the huxtable package, the more I like it. You may want to have a look.

2 Likes

Thanks for the advice again @pomchip! I've considered huxtable before but I have a problem finding that one R package that can do all the things I need to make nice formatted tables. I'm testing a bunch packages of packages to find the right one. I haven't gotten to huxtable yet but I'll be sure to give it a try!

You might want to check out gt. It's good at grouped headers, I believe. Meanwhile, here's a huxtable example:

library(huxtable)
library(dplyr)
as_hux(head(mtcars)) %>% 
    insert_row("MPG-DISP", "", "", "HP-WT", fill = "") %>% 
    merge_cells(1, 1:3) %>% 
    merge_cells(1, 4:6) %>%  
    set_header_rows(1, TRUE) %>% 
   style_headers(bold = TRUE)

which produces:

  MPG-DISP            HP-WT                           
   mpg   cyl   disp    hp   drat     wt   qsec    vs  
  21       6    160   110   3.9    2.62   16.5     0  
  21       6    160   110   3.9    2.88   17       0  
  22.8     4    108    93   3.85   2.32   18.6     1  
  21.4     6    258   110   3.08   3.21   19.4     1  
  18.7     8    360   175   3.15   3.44   17       0  
  18.1     6    225   105   2.76   3.46   20.2     1  

and which prints nicely to HTML or PDF. You can style further at will.

1 Like

Thanks @hughjonesd,

I considered using gt, but, IMO, huxtable currently provides more functionality than gt (in particular the range of possible outputs and the merging capabilities within the table itself).

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