首页 > 解决方案 > 为使用 reactable 创建的表添加标题

问题描述

我创建了一个包含可反应包的表,但我不确定如何为其添加标题。我在 Github 中有几个例子,他们添加了一个,但我无法达到相同的结果。这是我的代码:

library(reactable)
library(htmltools)
example<- reactable(data.frame(country=c("argentina","brazil"),
                     value=c(1,2)
                     ))
div(class = "table",
    div(class = "title", "Top CRAN Packages of 2019"),
    example
)
print(example)

这是一个示例,他们在表格中添加标题以及我曾经尝试添加标题的其他内容:

https://glin.github.io/reactable/articles/twitter-followers/twitter-followers.html

标签: r

解决方案


reactable函数返回一个特殊对象,该对象恰好使用htmlwidgets包来帮助渲染。您可以使用该htmlwidgets::prependContent函数将 HTML 内容添加到标题的对象中。例如

withtitle <- htmlwidgets::prependContent(example, 
    h2(class = "title", "Top CRAN Packages of 2019"))

print(withtitle)

<h2>会在打印时将标题标签和内容放在表格元素之前。


推荐阅读