插入存储在数据帧列表和绘图列表中的许多表和图表,以生成word文档

插入存储在数据帧列表和绘图列表中的许多表和图表,以生成word文档,r,dictionary,lapply,officer,R,Dictionary,Lapply,Officer,我想使用flexatable和officeR获取数据帧列表中的每个数据帧,并将一系列图表作为绘图列表存储到word文档(docx)中 目前,我一次只对一个数据帧执行此操作,但希望简化此过程 我的数据帧列表的一部分 l1<-list(Fe = structure(list(Determination_No = c("Count", "Min", "Max", "Median", "Mean"

我想使用flexatable和officeR获取数据帧列表中的每个数据帧,并将一系列图表作为绘图列表存储到word文档(docx)中

目前,我一次只对一个数据帧执行此操作,但希望简化此过程

我的数据帧列表的一部分

l1<-list(Fe = structure(list(Determination_No = c("Count", "Min", 
"Max", "Median", "Mean", "Std Dev", "Coeff. Variation", "Dev. From Cert Mean", 
"95% Confidence Interval", "SD Within Labs", "SD Between Labs", 
"MScore", "MIQR"), `2` = c("0", "   NA", "   NA", "   NA", "  NaN", 
"   NA", "  NA", "  NaN", "", "", "", "3.17", "6.93"), `3` = c("6", 
"56.18", "56.83", "56.50", "56.48", "0.218", "0.39", " 0.13", 
"", "", "", "0.92", "0.06"), `4` = c("6", "56.31", "56.53", "56.41", 
"56.41", "0.080", "0.14", " 0.01", "", "", "", "0.21", "1.35"
), `5` = c("6", "56.29", "56.39", "56.32", "56.33", "0.034", 
"0.06", "-0.15", "", "", "", "0.47", "3.62"), `7` = c("6", "56.40", 
"56.73", "56.51", "56.53", "0.125", "0.22", " 0.22", "", "", 
"", "0.96", "0.06"), `8` = c("6", "56.26", "56.53", "56.32", 
"56.36", "0.116", "0.20", "-0.09", "", "", "", "0.45", "0.31"
), `10` = c("6", "56.20", "56.70", "56.50", "56.45", "0.176", 
"0.31", " 0.08", "", "", "", "0.88", "0.12"), `12` = c("6", "56.10", 
"56.46", "56.36", "56.29", "0.150", "0.27", "-0.21", "", "", 
"", "0.21", "1.04"), Overall = c("42", "56.10", "56.83", "56.41", 
"56.41", "0.153", "0.27", "", "0.08", "0.14", "0.22", "3", "12"
)), class = "data.frame", row.names = c("LabsampSum", "LabMinSummary", 
"LabMaxSummary", "LabMedianSummary", "LabMeanSummary", "lab.SDSummary", 
"cv.summmary", "LabDevMean.Summary", "analyte.CI.Summary", "Lab.SDwithinLab.Summary", 
"Lab.SDBetweenLab.Summary", "X", "X.1")), SiO2 = structure(list(
    Determination_No = c("Count", "Min", "Max", "Median", "Mean", 
    "Std Dev", "Coeff. Variation", "Dev. From Cert Mean", "95% Confidence Interval", 
    "SD Within Labs", "SD Between Labs", "MScore", "MIQR"), `2` = c("6", 
    "7.63", "7.73", "7.67", "7.67", "0.033", "0.44", "-1.09", 
    "", "", "", "1.03", "1"), `3` = c("6", "7.59", "7.84", "7.72", 
    "7.71", "0.091", "1.18", "-0.55", "", "", "", "0.37", "0.66"
    ), `4` = c("6", "7.62", "7.81", "7.70", "7.72", "0.079", 
    "1.02", "-0.48", "", "", "", "0.51", "0.87"), `5` = c("6", 
    "7.84", "7.96", "7.89", "7.89", "0.048", "0.61", " 1.75", 
    "", "", "", "2.2", "0.04"), `7` = c("6", "7.65", "7.83", 
    "7.76", "7.76", "0.060", "0.77", " 0.01", "", "", "", "0.37", 
    "0.69"), `8` = c("6", "7.68", "7.94", "7.83", "7.82", "0.086", 
    "1.10", " 0.84", "", "", "", "1.36", "0.04"), `10` = c("6", 
    "7.62", "7.87", "7.79", "7.77", "0.111", "1.43", " 0.19", 
    "", "", "", "0.69", "1.81"), `12` = c("6", "7.64", "7.82", 
    "7.70", "7.70", "0.065", "0.84", "-0.68", "", "", "", "0.66", 
    "0.34"), Overall = c("48", "7.59", "7.96", "7.74", "7.76", 
    "0.097", "1.25", "", "0.06", "0.08", "0.18", "3", "12")), class = "data.frame", row.names = c("LabsampSum", 
"LabMinSummary", "LabMaxSummary", "LabMedianSummary", "LabMeanSummary", 
"lab.SDSummary", "cv.summmary", "LabDevMean.Summary", "analyte.CI.Summary", 
"Lab.SDwithinLab.Summary", "Lab.SDBetweenLab.Summary", "X", "X.1"
)), Al2O3 = structure(list(Determination_No = c("Count", "Min", 
"Max", "Median", "Mean", "Std Dev", "Coeff. Variation", "Dev. From Cert Mean", 
"95% Confidence Interval", "SD Within Labs", "SD Between Labs", 
"MScore", "MIQR"), `2` = c("6", "2.00", "2.03", "2.01", "2.01", 
"0.010", "0.52", "-0.16", "", "", "", "0", "0"), `3` = c("6", 
"2.00", "2.03", "2.01", "2.01", "0.010", "0.52", "-0.16", "", 
"", "", "0", "0"), `4` = c("6", "1.99", "2.03", "2.01", "2.01", 
"0.013", "0.66", "-0.49", "", "", "", "0.45", "0.67"), `5` = c("6", 
"2.02", "2.05", "2.02", "2.03", "0.012", "0.58", " 0.50", "", 
"", "", "0.9", "0"), `7` = c("0", "  NA", "  NA", "  NA", " NaN", 
"   NA", "  NA", "  NaN", "", "", "", "12.14", "0.67"), `8` = c("6", 
"2.01", "2.05", "2.04", "2.03", "0.017", "0.82", " 0.78", "", 
"", "", "2.2", "2.36"), `10` = c("6", "1.98", "2.02", "2.01", 
"2.01", "0.015", "0.77", "-0.45", "", "", "", "0.24", "2.4"), 
    `12` = c("0", "  NA", "  NA", "  NA", " NaN", "   NA", "  NA", 
    "  NaN", "", "", "", "3.6", "12.14"), Overall = c("36", "1.98", 
    "2.05", "2.01", "2.02", "0.016", "0.77", "", "0.01", "0.01", 
    "0.03", "3", "12")), class = "data.frame", row.names = c("LabsampSum", 
"LabMinSummary", "LabMaxSummary", "LabMedianSummary", "LabMeanSummary", 
"lab.SDSummary", "cv.summmary", "LabDevMean.Summary", "analyte.CI.Summary", 
"Lab.SDwithinLab.Summary", "Lab.SDBetweenLab.Summary", "X", "X.1"
)))
并且确实从Flexatable生成了一组标准表,但是,我在传递更多函数时遇到了问题

我可以

table <- map(df.summary,flextable) #this works
table <- map(table,autofit)
table <- map(table,lapply(line_spacing(table, space = 1, part = "all")))
表格
mycompound <- names(df)
makeTable <- function(compound){
  table <- flextable(compound)
  table <- autofit(table)
  table <- line_spacing(table, space = 1, part = "all")
  table <- bg(table, bg = "white", part = "header")
  table <- fontsize(table,size = 8,part = "all")
  table <- set_table_properties(table,width = 1, layout = "autofit")
  
  Appendix <- read_docx()
  
  Appendix <- body_add_flextable(Appendix,value = table,align = "center")
  Appendix <- body_add_par(Appendix,"")
  Appendix <- body_add_img(Appendix,src = paste0(compound,"_box.png",
                                                 width = 6.0, height = 2.3))
  Appendix <- body_add_par(Appendix,"")
  Appendix <- body_add_img(Appendix,src = paste0(compund,"_QQplot.png",
                                                  width = 6.0, height = 2.3))
  Appendix <- body_add_break(Appendix)
  print(Appendix, target = "Appendix.docx")
                                                    
}

lapply(mycompound,makeTable)

map(l1,flextable)#this works
table <- map(df.summary,flextable) #this works
table <- map(table,autofit)
table <- map(table,lapply(line_spacing(table, space = 1, part = "all")))
mycompound <- names(df)
Appendix <- read_docx()
makeTable <- function(mycompound){
  table <- flextable(mycompound)
  table <- autofit(table)
  table <- line_spacing(table, space = 1, part = "all")
  table <- bg(table, bg = "white", part = "header")
  table <- fontsize(table,size = 8,part = "all")
  table <- set_table_properties(table,width = 1, layout = "autofit")
  
  Appendix <- body_add_flextable(Appendix,value = table,align = "center")
  Appendix <- body_add_par(Appendix,"")
  Appendix <- body_add_img(Appendix,src = paste(mycompound,"_box.png",
                                               width = width, height = height))
  Appendix <- body_add_par(Appendix,"")
  #Appendix <- body_add_img(Appendix,src = paste0(compound,"_QQplot.png",
    #                                             width = 6.0, height = 2.3))
  Appendix <- body_add_break(Appendix)
  print(Appendix, target = "Appendix.docx")
  
}

Map(makeTable,df)