DT :: datatable显示

时间:2015-02-25 21:44:41

标签: r datatables shiny

在以下代码中,当取消注释“container = ...”行时,列名称会消失。

require(DT)

DT::datatable(cars[1:5,]
    #, container=htmltools::tags$table(class="display")
)

在浏览器中查看页面源,区别如下。容器评论:

"container": "<table>\n  <thead>\n    <tr>\n      <th>speed</th>\n      <th>dist</th>\n    </tr>\n  </thead>\n</table>",

未评论的容器:

"container": "<table class=\"display\"></table>",

知道如何让两者都起作用吗?

1 个答案:

答案 0 :(得分:2)

如果更改container选项,则会删除列名称。 如果您查看函数代码here,您可以看到第75行未设置container时会发生什么:

  if (missing(container)) container = tags$table(
    id = id,
    tags$thead(tags$tr(lapply(escapeColNames(colnames, escape), tags$th)))
  )

如果缺少container选项,则会生成包含标题的默认容器。 escapeColNames函数稍后定义,它仅用于清理标题名称。

如果设置了container选项,则不运行此代码,容器就是您在选项中提供的内容,因此您必须自己添加列名称。

例如你可以做(​​没有任何转义):

 DT::datatable(cars[1:5,],
    container=htmltools::tags$table(class="display",
          tags$thead(tags$tr(lapply(as.list(colnames(cars)), tags$th))))
)
相关问题