Search code examples
rheaderflextable

How to create a flextable with intermediare inner header lines


I would like to create one table in a way to enter the inner header between the two:

flextable(rbind(mtcars[,1:6], mtcars[,1:6]))

To make clear the idea it would be like to merge this two tables

ft = mtcars[,1:6] %>% 
  flextable()

ft1 = mtcars[,1:6] %>% 
  flextable() %>% 
  add_header_lines(., values = paste0('Results for XXXX'))

Do you possibly know what to do? Thanks


Solution

  • As far as I know there is no function for this, but we can make the header manually:

    library(dplyr)
    library(flextable)
    
    n_header <- nrow(mtcars)
    
    # I don't know if we can add lines in specific places but I know we can change them
    # so we add a empty line with "NA" where the header should be and change it after
    
    flextable(rbind(mtcars[,1:6],NA, mtcars[,1:6])) %>% 
      mk_par(i = n_header, value = as_paragraph("Inner Header")) %>% 
      bold(i = n_header) %>% 
      merge_h(i = n_header) %>% 
      align(i = n_header, align = 'center') %>% 
      border(i = n_header, border.top = officer::fp_border(), border.bottom = officer::fp_border())