2

我一直在尝试使用 xtable 和 hwriter R 包将列表转换为 HTML 表。列表如下所示:

structure(list(sex = structure(list(`study 1` = structure(c(2L, 
1L), .Label = c("111 (55.5%)", "89 (44.5%)"), class = "factor"), 
`study 2` = structure(1:2, .Label = c("29 (30.21%)", "67 (69.79%)"
), class = "factor"), `study 3` = structure(c(2L, 1L), .Label = c("112 (56.85%)", 
"85 (43.15%)"), class = "factor"), `study 113220` = structure(1:2, .Label = 
c("132 (44.75%)", "163 (55.25%)"), class = "factor")), .Names = c("study 1", 
"study 2", "study 3", "study 113220"), class = "data.frame", row.names = c("0", 
"1")), ethnic = structure(list(`study 1` = structure(c(2L, 1L
), .Label = c("194 (97%)", "6 (3%)"), class = "factor"), `study 2` = 
structure(1:2, .Label = c("2 (2.08%)", "94 (97.92%)"), class = "factor"), 
`study 3` = structure(c(2L,1L), .Label = c("194 (98.48%)", "3 (1.52%)"), class = 
"factor"),  `study 113220` = structure(1:2, .Label = c("10 (3.39%)", 
"285 (96.61%)"), class = "factor")), .Names = c("study 1", 
"study 2", "study 3", "study 113220"), class = "data.frame", row.names = c("1", 
"2")), race = structure(list(`study 1` = structure(c(4L, 1L, 
1L, 3L, 3L, 2L, NA, NA, NA, NA), .Label = c("1 (0.5%)", "187 (93.5%)", 
"2 (1%)", "7 (3.5%)"), class = "factor"), `study 2` = structure(c(NA, 
NA, NA, NA, 1L, 2L, NA, NA, NA, NA), .Label = c("1 (1.04%)", 
"95 (98.96%)"), class = "factor"), `study 3` = structure(c(NA, 
NA, NA, NA, 2L, 1L, NA, NA, NA, NA), .Label = c("195 (98.98%)", 
"2 (1.02%)"), class = "factor"), `study 113220` = structure(c(1L, 
NA, 1L, NA, NA, 2L, 1L, 1L, 1L, 1L), .Label = c("1 (0.34%)", 
"289 (97.97%)"), class = "factor")), .Names = c("study 1", "study 2", 
"study 3", "study 113220"), class = "data.frame", row.names = c("11", 
"14", "15", "17", "18", "19", "13", "16", "99", "NA"))), .Names = c("sex", 
"ethnic", "race"))

下面是我如何创建我想出的 HTML 表:

writeHTML = function(list,file){
  widthpx = sprintf("%d",100*(1+ncol(list[[1]])))
  vect = sapply(1:length(x), function(i) hwrite(list[[i]],
                                                center=T, 
                                                table.style="border-collapse:collapse",
                                                table.frame="void",
                                                table.rules="all",
                                                border=1,
                                                row.names=T, 
                                                cellpadding="5px",
                                                row.style=list("font-weight:bold"),
                                                width=widthpx,
                                                col.width="70px") )
  names(vect) = names(list)
  hwrite(vect, page=file, center=T, 
         table.style="border-collapse:collapse",
         border=4, 
         row.names=T, 
         col.style=list("font-weight:bold"),
         dim=c(length(vect),1))
}
writeHTML(x,"x.html")

但列未对齐....

任何人都知道如何解决这个问题?谢谢!!!

4

0 回答 0