首页 > 解决方案 > 将 ftable 因子导出到 html

问题描述

我有一个从创建的表ftable()

structure(c(1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L, 0L, 2L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L), .Dim = c(12L, 7L), class = "ftable", row.vars = list(
    ï..petal_size = c("large ", "small", "small "), stem_length = c("long", 
    "long ", "short", "short ")), col.vars = list(flow_color = c("blue", 
"green", "indigo ", "orange", "red  ", "violet", "yellow")))

我想使用 导出它htmlTable,但是当我使用它时htmlTable,我得到的结果没有任何因素,只有图片中的数字

在此处输入图像描述

如何恢复 htmltable 的因子名称?请注意,最终输出的行数和列数应与图片的输出相同,但行和列上需要有因子名称。

标签: rhtml-tablefactors

解决方案


我将首先将其转换为data.frame并添加必要的调整以获得所需的输出:

tableToHtml <-structure(c(1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
            0L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
            1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
            0L, 1L, 0L, 2L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
            0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 
            0L, 0L, 1L, 0L, 0L, 0L, 0L), .Dim = c(12L, 7L), class = "ftable", row.vars = list(
              ï..petal_size = c("large ", "small", "small "), stem_length = c("long", 
                                                                              "long ", "short", "short ")), col.vars = list(flow_color = c("blue", 
                                                                                                                                           "green", "indigo ", "orange", "red  ", "violet", "yellow")))

 library(htmlTable)

 htmlTable(as.data.frame(tableToHtml),rnames=F, header=rep("", length(colnames(as.data.frame(tableToHtml)))))

推荐阅读