setting a new header to the table dynamically from the labels of banner

I have a function which is creating table one by one for the list of banner. but now want to show them as binding as cbind.

so i am able to bind the table by columns but not able to make banners as headers for table. also i want to remove names like n.x, n.y . just wanted to keep them N and i needed a flextable output

currently table is coming like below
image

library(knitr)
library(tidyverse)
library(flextable)

banner <- c("All","Other")
t1 <- structure(list(` ` = c("CA", "USA","UK", "GER", "Italy","France", "China"),
                                               Local = c("38%", "58%","71%", "78%", "91%", "91%", "84%"),
                                               Outsider = c("43%", "28%", "29%"," 9%", " 7%", " 4%", " 5%"), 
                                               Mixed = c("19%","13%", " 0%", "13%", " 1%", " 5%", "11%"),
                                               N = c("9999", "9999","9999", "9999", "9999", "9999", "9999")), row.names = c(NA, -7L), class = "data.frame")

t2 <- structure(list(` ` = c("CA", "USA","UK", "GER", "Italy","France", "China"),
                     Local = c("71%", "93%","96%", "96%", "96%", "96%", NA),
                     Outsider = c(" 7%", " 4%", " 4%"," 0%", " 0%", " 0%", NA),
                     Mixed = c("21%"," 4%", " 0%", " 4%", " 4%", " 4%", NA),
                     N = c("2800", "2800","2800", "2800", "2800", "2800", NA)), row.names = c(NA, -7L), class = "data.frame")
tl <- list(t1,t2)


fun1 <- function(tl,banner){
  t_list1<-list()
  for (i in 1:length(banner)) 
  {
    t_list <- list()
    t_list1[[i]] <- tl[[i]]
  }
  t1<-Reduce(function(...)merge(...,all=TRUE,by=" "),t_list1)
  
  t1 <- t1 %>% flextable()
  t1
}

fun1(tl=tl,banner = banner)

the output should be a flextable and should look like below because i will render the output in word

image

options(tidyverse.quiet = TRUE)
library(tidyverse)
library(flextable)
library(glue)

banner <- c("All","Other")
t1 <- structure(list(` ` = c("CA", "USA","UK", "GER", "Italy","France", "China"),
                     Local = c("38%", "58%","71%", "78%", "91%", "91%", "84%"),
                     Outsider = c("43%", "28%", "29%"," 9%", " 7%", " 4%", " 5%"), 
                     Mixed = c("19%","13%", " 0%", "13%", " 1%", " 5%", "11%"),
                     N = c("9999", "9999","9999", "9999", "9999", "9999", "9999")), row.names = c(NA, -7L), class = "data.frame")

t2 <- structure(list(` ` = c("CA", "USA","UK", "GER", "Italy","France", "China"),
                     Local = c("71%", "93%","96%", "96%", "96%", "96%", NA),
                     Outsider = c(" 7%", " 4%", " 4%"," 0%", " 0%", " 0%", NA),
                     Mixed = c("21%"," 4%", " 0%", " 4%", " 4%", " 4%", NA),
                     N = c("2800", "2800","2800", "2800", "2800", "2800", NA)), row.names = c(NA, -7L), class = "data.frame")
tl <- list(t1,t2)

fun1 <- function(tl,banner){
  
  table_info <- map2_dfr(tl,banner,\(t,b){
    tibble(colw = ncol(t),
           val = b)
  })
  
  
  bvalues <- table_info$val
  bwidths <- table_info$colw
  
  joined_tabs <- do.call(cbind,args = tl)
  prefered_head <- names(joined_tabs)
  names(joined_tabs) <- seq_along(names(joined_tabs))
  a1 <- flextable(joined_tabs)
  b2 <- a1 |> set_header_labels(values =
                                  prefered_head)
  c3 <- b2 |>  add_header_row(values=bvalues,
                              colwidths = bwidths)
  
  c3 |> align(
   
    align = c("center"),
    part = "header"
  )
  
}

fun1(tl,banner)

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.