【问题标题】:R shiny mouseover text for table columns表格列的R闪亮鼠标悬停文本
【发布时间】:2015-09-16 09:57:10
【问题描述】:

如何为 R 闪亮数据表显示中的列名创建 mouseover text。 我正在尝试为用户提供一些文本以了解列名。 我也签入了 DT 包,但找不到解决方案。 我可以为列名创建标签,并在用户选中一个框时显示所有这些标签,这会占用大量空间,我不希望这样。 有什么建议吗?

【问题讨论】:

标签: r shiny dt


【解决方案1】:

为了扩展我上面的评论,下面是一个示例,显示了我使用 title 属性的含义:

library(DT)
sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th('', title = 'Row Names'),
      th('Sepal.Length', title = 'The Sepal Length'),
      th('Sepal.Width', title = 'The Sepal Width'),
      th('Petal.Length', title = 'The Petal Length'),
      th('Petal.Width', title = 'The Petal Width'),
      th('Species', title = 'Iris Species')
    )
  )
))
datatable(iris, container = sketch)

这是另一种使用 JavaScript (jQuery) 添加 title 属性的方法:

library(DT)
datatable(iris, callback = JS("
var tips = ['Row Names', 'The Sepal Length', 'The Sepal Width',
            'The Petal Length', 'The Petal Width'],
    header = table.columns().header();
for (var i = 0; i < tips.length; i++) {
  $(header[i]).attr('title', tips[i]);
}
"))

【讨论】:

  • 感谢您的替代方法。
  • 谢谢@Yihui ...将鼠标悬停到所有表格单元格而不是标题的相应方法是什么?我已经尝试了很多,但到目前为止我还没有成功...谢谢
  • jquery 版本似乎对我不起作用——当我试图包含这个 sn-p 时,我的表根本没有加载。所以我改用容器方法,这很有效。但是我之前使用tableHeader(table,escape= F) 来定义我的列名。我怎样才能转义标题名称?
  • 第一种方法对我有用。第二种方法(jquery)没有。谢谢!
【解决方案2】:

您可能可以在 Shiny 的 renderDataTable() 函数中使用 options 来完成此操作。从 Shiny 中 DT 的documentation 页面,这样的东西应该可以工作。

renderDataTable(head(iris, 20), options = list(
  initComplete = JS(
    "function(settings, json) {",
        "$(this.api().table().header()).on({
            mouseenter: function () {
                //stuff to do on mouse enter
            },
            mouseleave: function () {
                //stuff to do on mouse leave
            }
         });",
    "}")
))

【讨论】:

  • 这可能适用于整个标题行的一个文本,但我正在为每个列名寻找一个单独的值。可以在renderDataTable 和HTML 标签中使用colnames 来完成一些事情吗?
猜你喜欢
  • 2017-03-06
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2012-02-29
  • 2021-07-25
  • 1970-01-01
  • 2019-06-08
相关资源
最近更新 更多