行名称的闪亮数据表中的工具提示或弹出窗口?

tooltip or popover in Shiny datatables for row names?

当用户将鼠标悬停在/单击数据表的行名称时,我一直在尝试添加诸如工具提示或弹出窗口之类的附加信息,这样他们就不必查找一些定义,这我目前在不同的 tabPanel 上。这是一个工作示例:

server.R:

library(shiny)
library(DT)
library(shinyBS)

# Define server for the Shiny app
shinyServer(function(input, output,session) {

tdata <- as.data.frame(iris)

# Render table here 
output$mytable <- DT::renderDataTable(DT::datatable(

   tdata[1:5,],

   options = list(paging = FALSE, searching = FALSE, info = FALSE, sort = FALSE,
                 columnDefs=list(list(targets=1:4, class="dt-right")) ),

   rownames = paste("rowname",1:5),

   container = htmltools::withTags(table(
      class = 'display',
      thead(
         tr(lapply(rep(c('ratios','name1', 'name2', 'name3','name4','name5'), 1),th))
      )
   ))
))

}) # end of shinyServer function

ui.R:

library(shiny)
library(DT)
library(shinyBS)

shinyUI(
   mainPanel(
      DT::dataTableOutput("mytable")
   )   
)      

请注意,我已经看到以下讨论主题,但没有成功: ,以及 所以我在考虑 DT 包选项中的某些内容,或者使用 shinyBS 包(如 'bsTooltip')或添加一些 HTML 或 JS 的内容。 这个 tooltip/popover 特性似乎并没有被 Shiny 在数据表中自然支持...!?

它不起作用,因为您的代码没有使用 title 属性,该属性用于在悬停时显示标签。

container = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(lapply(rep(c('ratios','name1', 'name2', 'name3','name4','name5'), 1),th))
  )
))
# OUTPUT OF CONTAINER
#<table class="display">
#  <thead>
#    <tr>
#      <th>ratios</th>
#      <th>name1</th>
#      <th>name2</th>
#      <th>name3</th>
#      <th>name4</th>
#      <th>name5</th>
#    </tr>
#  </thead>
#</table>

我将您的代码更改为以下内容(使用 title 属性),现在它应该可以工作了:

标签设置为 columnLabels <- paste0("label", 1:6),除此之外,仅更改了容器。

  # Render table here 
  output$mytable <- DT::renderDataTable({
    columnLabels <- paste0("label", 1:6)

    DT::datatable(
      tdata[1:5,],

      options = list(paging = FALSE, searching = FALSE, info = FALSE, sort = FALSE,
                     columnDefs=list(list(targets=1:4, class="dt-right")) ),

      rownames = paste("rowname",1:5),

      container = htmltools::withTags(table(
        class = 'display',
        thead(
          #tags$th(title=active_columns[i], colnames(data)[i])
          tr(apply(data.frame(colnames=c('ratios','name1', 'name2', 'name3','name4','name5'), labels=columnLabels), 1,
                   function(x) th(title=x[2], x[1])))
        )
      ))
    )
  })
# OUTPUT OF CONTAINER
#<table class="display">
#  <thead>
#    <tr>
#      <th title="label1">ratios</th>
#      <th title="label2">name1</th>
#      <th title="label3">name2</th>
#      <th title="label4">name3</th>
#      <th title="label5">name4</th>
#      <th title="label6">name5</th>
#    </tr>
#  </thead>
#</table>

此代码有效,但 运行 在客户端模式下。为了简单起见,我使用了 iris 数据集的前五行,但我想这个想法很清楚。如果将鼠标悬停在行名称上,将显示工具提示。

ui.R

    library(shiny)
    library(DT)
    shinyUI(
            mainPanel(
                    DT::dataTableOutput("tbl")
            )   
    )    

server.R

    library(shiny)
    library(DT)
    shinyServer(function(input, output,session) {
            output$tbl = DT::renderDataTable(
                    datatable(iris[1:5, ], callback = JS("
                                    var tips = ['First row name', 'Second row name', 'Third row name',
                                    'Fourth row name', 'Fifth row name'],
                                    firstColumn = $('#tbl tr td:first-child');
                                    for (var i = 0; i < tips.length; i++) {
                                    $(firstColumn[i]).attr('title', tips[i]);
                                    }")), server = FALSE)
    })