Search code examples
rloopsflextable

How do I get my flextable to retain the names of elements in the list that it draws from in R?


My flextable function keeps replacing the element names of table_list with "var1", "var2" etc. I don't want that.

Here is my data:

table_list<-list(A03.PREDIABETES = structure(list(Var1 = structure(1:2, levels = c("N", 
"Y"), class = "factor"), Freq = c(8L, 3L)), class = "data.frame", row.names = c(NA, 
-2L)), A03.GOITER = structure(list(Var1 = structure(1L, levels = "N", class = "factor"), 
    Freq = 11L), class = "data.frame", row.names = c(NA, -1L)), 
    A03.GESTATIONAL = structure(list(Var1 = structure(1:2, levels = c("N", 
    "Y"), class = "factor"), Freq = c(9L, 2L)), class = "data.frame", row.names = c(NA, 
    -2L)), A03.HASHIMOTO = structure(list(Var1 = structure(1L, levels = "N", class = "factor"), 
        Freq = 11L), class = "data.frame", row.names = c(NA, 
    -1L)), A03.THYROID = structure(list(Var1 = structure(1:2, levels = c("N", 
    "Y"), class = "factor"), Freq = c(10L, 1L)), class = "data.frame", row.names = c(NA, 
    -2L)), A03.HYPERTHYROID = structure(list(Var1 = structure(1:2, levels = c("N", 
    "Y"), class = "factor"), Freq = c(10L, 1L)), class = "data.frame", row.names = c(NA, 
    -2L)), A03.HYPOTHYROID = structure(list(Var1 = structure(1L, levels = "N", class = "factor"), 
        Freq = 11L), class = "data.frame", row.names = c(NA, 
    -1L)), A03.OTHER = structure(list(Var1 = structure(1:2, levels = c("N", 
    "Y"), class = "factor"), Freq = c(10L, 1L)), class = "data.frame", row.names = c(NA, 
    -2L)), A03.NONE = structure(list(Var1 = structure(1:2, levels = c("N", 
    "Y"), class = "factor"), Freq = c(7L, 4L)), class = "data.frame", row.names = c(NA, 
    -2L)))

Here is my code:

library(data.table)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(writexl)
library(anytime)
library(officer)
library(flextable)

# Create a Word document
doc <- read_docx()

# Add each table to the Word document
for (i in 1:length(table_list)) {
  df <- table_list[[i]]
  table <- flextable(df)
  table <- set_table_properties(table, width = .5)
  doc <- doc %>% body_add_flextable(table)
}

# Save the Word document
print(doc, target = "report.docx")

Specifically, the labels are lost here: for (i in 1:length(table_list)) { df <- table_list[[i]] I think it has to do with the loop accessing things through numbers rather than names. I've tried iterating over things using element names. But if that is the solution, I haven't figured out how to do it correctly.


Solution

  • You can add labels as captions for each table. This can be done using flextable::set_caption.

    library(officer)
    library(flextable)
    
    doc <- read_docx()
    
    for(i in 1:length(table_list)){
      tbl <- table_list[[i]] |> 
        flextable() |> 
        set_table_properties(width = 0.5) |> 
        set_caption(names(table_list)[[i]])
      
      doc <- doc |> body_add_flextable(tbl)
    }
    
    print(doc, target = "report.docx")
    

    output:

    enter image description here

    enter image description here