如何让我的 flextable 保留它在 R 中绘制的列表中的元素名称?

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

提问人:stephr 提问时间:11/3/2023 最后编辑:stephr 更新时间:11/3/2023 访问量:69

问:

我的 flextable 函数不断将 table_list 的元素名称替换为“var1”、“var2”等。我不想那样。

这是我的数据:

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)))

这是我的代码:

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")

具体来说,标签在这里丢失了: for (i in 1:length(table_list)) { df <- table_list[[i]] 我认为这与通过数字而不是名称访问事物的循环有关。 我尝试过使用元素名称遍历事物。但如果这是解决方案,我还没有弄清楚如何正确地做到这一点。

R 循环可 弯曲

评论

0赞 nightstand 11/3/2023
您的数据框是如何定义的?baseline
0赞 stephr 11/3/2023
这些列具有我要应用的标签。但是,当事物转换为列表时,元素会丢失名称并采用通用名称。我成功地重新应用了此处的名称:table_list<-setNames(table_list, labels$V1)。但是,它们在这一部分中再次丢失:for (i in 1:length(table_list)) { df <- table_list[[i]]。或者你是在要求提供数据?如果这是你要问的,我会得到你的dput摘要。
0赞 nightstand 11/3/2023
是的,我要求提供 dput 摘要。没有它,我无法运行您的代码。此外,如果您可以将所需的输出添加到您的问题中,那将会很有帮助
0赞 nightstand 11/3/2023
此时,您的代码不会显示您对 的每个数据框中的变量标签所做的任何更改。您只需将每个数据框转换为可弯曲的对象并将其保存在 word 文档中,而无需像您在问题中提到的那样对标签进行任何修改。我强烈建议您阅读这篇文章,了解如何创建一个可重现的示例table_list
0赞 stephr 11/3/2023
也许问题在于我是如何谈论它的。我现在已经删除了一些我仅出于上下文考虑而提供的代码,并且还添加到了库中。不过,该问题确实存在于提供的代码中。这些元素在我提供的 dput 中具有名称(即“A03.妊娠”。它们在以下行中被删除:for (i in 1:length(table_list)) { df <- table_list[[i]]。

答:

2赞 nightstand 11/3/2023 #1

您可以为每个表添加标签作为标题。这可以使用 来完成。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")

输出:

enter image description here

enter image description here