如何在DT标头中显示HTML?

时间:2018-12-13 19:01:16

标签: r dt

我想在DT数据表的标题中放入一些HTML。我所有的尝试都失败了:标签按“原样”呈现。这是一个可重现的示例:

> dput(dd)
structure(list(plate = c("P1", "P1", "P1"), row = c(1, 2, 3), 
    Rsquared0 = c("0.3", "45.8", "11.7"), Rsquared1 = c("0.3", 
    "51.1", "20.3"), adjRsquared1 = c("-21.8", "40.2", "2.6"), 
    pvalue = c("99.8", "35.2", "35.1")), .Names = c("plate", 
"row", "Rsquared0", "Rsquared1", "adjRsquared1", "pvalue"), row.names = c(NA, 3L), class = "data.frame")

library(DT)
sketch = htmltools::withTags(
  table(
    class = "display",
    thead(
      tr(
        th(colspan = 2, "Factors"),
        th(colspan = 4, "Statistics")
      ),
      tr(
        lapply(c(names(results)[1:2], 
                 "<i>R</i><sup>2</sup> linear",
                 "<i>R</i><sup>2</sup> quadratic",
                 "adj. <i>R</i><sup>2</sup> quadratic",
                 "<i>p</i> quadratic term"
        ), th)
      )
    )
  )
)

datatable(dd, container = sketch, escape = FALSE)

enter image description here

1 个答案:

答案 0 :(得分:2)

请参见?htmltools::tags:要使用原始HTML,您需要HTML。例如,

lapply(lapply(c(names(results)[1:2], 
                "<i>R</i><sup>2</sup> linear",
                "<i>R</i><sup>2</sup> quadratic",
                "adj. <i>R</i><sup>2</sup> quadratic",
                "<i>p</i> quadratic term"
), HTML), th)

enter image description here