creating list of tables created by functions

I am trying to create a list of tables created by output of function. i have tried many ways but not working.

 df <- 
      mtcars[1:8,4:ncol(mtcars)] %>% 
      rownames_to_column(var = "model")
    
    
    df1 <- subset(df, vs==1)
    df2 <- subset(df, am==1)
    df3 <- subset(df, gear==3)
    
    df_list <- list(df1,df2,df3)
    banner <- c("T1","T2","T3")
    
    
    func1<-function(df,list_var,....){
      
        table_list<-list()
        for (i in 1:length(df_list)) {
          
          table_list[[i]]<-sub_fun(df, list_var[i])
          
          t1 <- do.call(rbind,table_list)
# this sub function is creating a frequency table for multiple columns 
# t1 is a table here and this loop create table like t1, t2 , t3          
        }
        
        colnames(t1)[1] <- banner[[d]]
# here i am changing the name of first row of table like "T1"
        
# my question is here i want to create a list of tables and convert them #into a single data frame of list of dataframes above
        table_list1[i] <- t1
        t2 <- t2 <- Reduce(bind_rows, table_list1)
      t2
    }

the output should be like a single data frame with one blank row between them. so for example if function produces three tables then output should be like three dataframes, one below another and that will become single dataframe final.

#Desired output enter image description here

is your purpose computation or presentation?
if your purpose is presentation then I think you should look at HTML type outputs perhaps with gt package
if your purpose is computation it likely wont serve you well to force all the data into a single standard R data.frame as this would imply conversion of your numeric values to characters as your quasi headers 'hp' 'drat' etc are of a different type than the values etc.

I just updated the question, the final output i will save in xlsx file . its just presentation.
no computation going forward. so the final object will the output

library(tidyverse)
(rowsizes_ <- map2(banner,
                   df_list,
                   ~ c(length(.x), nrow(.y) + 2)))

(rowstarts <- c(1, flatten(rowsizes_)) %>%
    head(-1) %>% 
    cumsum())

(interleaved_content <- c(rbind(banner,
                                df_list)))

library(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "Sheet 1")

walk2(
  .x = rowstarts,
  .y = interleaved_content,
  .f = ~ {
    writeData(
      wb,
      "Sheet 1",
      .y,
      startCol = 1,
      startRow = .x
    )
  }
)

saveWorkbook(wb, "mywb.xlsx", overwrite = TRUE)

This topic was automatically closed 21 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.