R shiny mouseover text for table columns

牧云@^-^@ 提交于 2019-11-27 05:37:41

问题


How can I create mouseover text for column names in R shiny data table display. I'm trying to provide some text for users to understand the column names. I checked in DT package also and I couldn't find a solution. I can create labels for column names and display all of them when a user checks a box, this takes a lot of real estate and I don't want that. Any tips?


回答1:


To expand my comment above, here is an example showing what I meant by using the title attributes:

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)

And here is another approach using JavaScript (jQuery) to add the title attributes:

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]);
}
"))



回答2:


You can probably accomplish that using optionsin the renderDataTable() function in Shiny. From the documentation page of DT in Shiny, something like this should work.

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
            }
         });",
    "}")
))


来源:https://stackoverflow.com/questions/31124122/r-shiny-mouseover-text-for-table-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!