Customizing headers in summary tables for results in combinations of variables

Hello, I am creating a set of tables (14 in total) describing the 'biopsy_result_1' results for each combination of 'triaje_anormal' and 'colpo_result_1' values.
How can I customize the tables so that their header clearly indicates which subset of the data they refer to.
This is my code:

base2<- 
  base_pos %>%
  filter(triaje_anormal %in% c(1, 99)) %>%
  group_split(triaje_anormal) %>%
  map(~ group_split(.x, colpo_result_1)) %>%
  map_depth(2, ~ .x %>% 
              select(biopsia_result_1) %>% 
              tbl_summary(missing = "always",
                          statistic = list(all_categorical() ~ "{n} ({p}"),
                          missing_text= "Casos perdidos",
                          digits = list(all_categorical() ~ c(0,1))) %>%
              gtsummary::as_tibble()) 

wb <- createWorkbook()

sheet_counter <- 1

for (i in seq_along(base2)) {
  for (j in seq_along(base2[[i]])) {
    # Crear el nombre de la hoja
    sheet_name <- 
      paste0("Table", sheet_counter)
    
    addWorksheet(wb, sheet_name)
    
    # Crear el título
    title <- paste("Tabla de biopsia_result_1 de mujeres positivas filtradas por el valor de triaje anormal", unique(base$triaje_anormal)[i], 
                   "y el valor de colpo_result_1", unique(base$colpo_result_1)[j])

    writeData(wb, sheet = sheet_name, title, startRow = 1, startCol = 1)
   
    writeData(wb, sheet = sheet_name, base2[[i]][[j]], startRow = 3, startCol = 1)

    sheet_counter <- 
      sheet_counter + 1
  }
}

saveWorkbook(wb, "Reportes/Cuadros/Utilidades/si_tareas_2024_07_22_2tablas_triaje_colpo.xlsx", overwrite = TRUE)

I would think that as you construct the tables, you can record into an attribute the table label you would want to use, and then later when you loop through, you can extract that and use that however.

for example

library(tidyverse)
library(gtsummary)

r1 <- mtcars |>
  group_split(am) |>
  map(~ group_split(.x, gear)) |>
  map_depth(2,\(x) {
    am_cat <- unique(pull(x,am))
    gear_cat <- unique(pull(x,gear))
    table_label <- paste0("am: ", am_cat," gear: ",gear_cat)
    table_result <- x |> 
              select(hp) |> 
              tbl_summary(missing = "always",
                          statistic = list(all_categorical() ~ "{n} ({p}"),
                          missing_text= "Casos perdidos",
                          digits = list(all_categorical() ~ c(0,1))) |> 
              gtsummary::as_tibble() 
    attributes(table_result) <- append(attributes(table_result),list(table_label=table_label))
    table_result
    }) 

# lets get the last table
r1[[2]][[2]]
#access the table_label on it 
attr(r1[[2]][[2]],which = "table_label")
# [1] "am: 1 gear: 5"

# lets get the first table
r1[[1]][[1]]
#access the table_label on it 
attr(r1[[1]][[1]],which = "table_label")
# [1] "am: 0 gear: 3"
1 Like

Hi, it doesn't work. The name isnt in the header of the table. I need it there.

My example doesnt work ? Or you need help writing the table header values into a cell of an excel sheet ?

When i refer to the header i mean the table header. And it shows 'Characterist' as header.

I dont understand your issue...
You havent been explicit whether my example runs on your machine and does what I expect it does.

I expect that my code does work and lets you associate arbitrary text (a title) as an attribute set on a data.frame.
Therefore its possible to have code that a) writes the data.frame into an excel sheet b) writes the title attribute associated with that excel sheet into a row/cell used for that purpose.

i.e. your code like

writeData(wb, sheet = sheet_name, title, startRow = 1, startCol = 1)
writeData(wb, sheet = sheet_name, base2[[i]][[j]], startRow = 3, startCol = 1)

could be more like

myframe<- base2[[i]][[j]]
mytitle<- attr(myframe,which = "table_label")
writeData(wb, sheet = sheet_name, mytitle, startRow = 1, startCol = 1)
writeData(wb, sheet = sheet_name, myframe, startRow = 3, startCol = 1)

Hi, look the output with your code.

> r1[[2]][[2]]
# A tibble: 7 × 2
  `**Characteristic**` `**N = 5**`
* <chr>                <chr>      
1 hp                   NA         
2 91                   1 (20,0    
3 113                  1 (20,0    
4 175                  1 (20,0    
5 264                  1 (20,0    
6 335                  1 (20,0    
7 Casos perdidos       0  

See how the leader of the table says '**Characteristic**'. What I need is instead the header indicates which subset of the data they refer to.

If you want to rename a colum of a data.frame you can do so with the names or colnames functions.

But how can I do in the code customized for each table?