I am trying to create a function which can save multiple tables in xl file one by one but there should be a one row gap between tables .
I tried two ways but nothing is working perfectly as required.
library(expss)
library(openxlsx)
tt <- list()
tt[[length(tt)+1]] = mtcars %>%
cross_cpct(
cell_vars = list(cyl, gear),
col_vars = list(total(), am, vs)
)
tt[[length(tt)+1]] = mtcars %>%
tab_cols(total(), am %nest% vs) %>%
tab_cells(mpg, hp) %>%
tab_stat_mean() %>%
tab_cells(cyl) %>%
tab_stat_cpct() %>%
tab_pivot()
brands = as.sheet(t(replicate(20,sample(c(1:5,NA),4,replace = FALSE))))
score = sample(-1:1,20,replace = TRUE)
var_lab(brands) = "Used brands"
val_lab(brands) = autonum("
Brand A
Brand B
Brand C
Brand D
Brand E
")
tt[[length(tt)+1]] <- cro_cpct(mrset(brands), score)
wb1 <- createWorkbook()
tabl_theme <- function(tbls, wb){
addWorksheet(wb, "Tables")
rows <- c(0, cumsum(2 + sapply(tbls, nrow)[-length(tbls)])) + 1
setColWidths(wb, 1,cols = 1:100, widths = "auto")
for(i in seq_along(tbls)){
xl_write(tbls[[i]],wb,1,row = rows[i])
}
return(wb)
}
tabl_theme(tbls=tt,wb=wb1)
saveWorkbook(wb1, "test3.xlsx", overwrite = TRUE)
file.show("test3.xlsx")
I also tried to save like below but this also doesn't work do we have any other solution or what i am doing wrong.
The objective is to save n number of tables in tt, so i have more than 500 list of tables in tt and i want to save them in xlsx file with xl_write format only. also there should be one row gap between all the tables.
saving_tabls <- function(tbls, wb){
addWorksheet(wb, "Tables")
start_col <- 1
cols_between_data <- 3
for(i in length(tbls)) {
xl_write(xt[[1]],wb,1,row = start_col)
}
start_col <- start_col + nrow(xt[[1]]) + cols_between_data
}