update function to have table cbind according to 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 it should be displayed horizontally according to banner.

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){
  for (i in 1:length(banner)) 
    {
    cat("\n \\newline \n")
    cat("\n\n### ", banner[[i]], "\n\n")
  t_list <- list()
  t1 <- tl[[i]]
  t1 <- t1 %>% flextable()
  cat(knit_print(t1))
}
  cat("\n\n")
}

fun1(tl=tl,banner = banner)

the output should look like
image

I would approach the issue like this:


---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

```{r echo=FALSE, warning=FALSE}
suppressMessages({
  library(knitr)
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){
map2(tl,banner,
     .f=\(t,b){
        lcol <- ncol(t)
 flextable:::to_html.flextable(flextable(t)|> add_header_row( values = as_paragraph(as_chunk(b)) ,colwidths = lcol, top = TRUE) |> align( align = "center", part = "header"))
     })
}

html_of_tables <- fun1(tl=tl,banner=banner)

```

```{r,results='asis',echo=FALSE}
glue("<HTML>
<div style='display:flex;'>
{paste0(html_of_tables,collapse = '\n')}
</div>
</HTML>")

A data frame is not an appropriate object in which to attempt formatting. {gt} will provide almost anything ever needed.

library(gt)
library(gtExtras)
t1 <- data.frame(
  Country = c("CA", "USA", "UK", "GER", "Italy", "France", "China"),
  Local = c(.38, .58, .71, .78, .91, .91, .84),
  Outsider = c(.43, .28, .29,.09,.07,.04,.05),
  Mixed = c(.19, .13,0, .13,.01,.05, .11),
  N = c(9999, 9999, 9999, 9999, 9999, 9999, 9999)
)

t2 <- data.frame(
  Country = 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)
)

tab1 <- t1 |> 
  gt(rowname_col = "Country") |>
  tab_header("All") |>
  fmt_number(columns = N, decimals = 0) |>
  fmt_percent(columns = c(Local, Outsider, Mixed), decimals = 0)

tab2 <- t2[-1] |>
  gt() |>
  tab_header("Other") |>
  fmt_number(columns = N, decimals = 0) |>
  fmt_percent(columns = c(Local, Outsider, Mixed), decimals = 0)

gt_two_column_layout(list(tab1,tab2))

gt() produces HTML which won't render here, so a screenshot is attached

but i want flextables output

I used flextables; each flextable has a header row banner added as you wanted.
To display in markdown the flextable has to knit to something viewable; in this example html.

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.