【问题标题】:Datatable tooltip from another column来自另一列的数据表工具提示
【发布时间】:2017-09-12 10:07:46
【问题描述】:

如何将工具提示(或鼠标悬停弹出窗口)添加到数据表的单元格中,以从另一列中提取数据?

例如,如果我在数据表中显示 mtcar 的前三列,我如何显示一个工具提示,其中包含我当前用鼠标悬停在的汽车名称的 hp(马力)数据?

similar questions on how you can display static text as a tooltip,但我找不到将另一列中的数据显示为工具提示的方法。

#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(mtcars[, 1:3]))
}) 

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    要使用 hp 获取工具提示,您可以按如下方式修改服务器代码:

       shinyServer(function(input, output,session) {
        output$tbl <- DT::renderDataTable({
          datatable(mtcars[, 1:4], options = list(rowCallback = JS(
            "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
            "var full_text = aData[4]",
            "$('td:eq(0)', nRow).attr('title', full_text);",
            "$('td:eq(1)', nRow).attr('title', full_text);",
            "$('td:eq(2)', nRow).attr('title', full_text);",
            "$('td:eq(3)', nRow).attr('title', full_text);",
            "}"),
    
            columnDefs = list(list(visible=FALSE, targets=c(4)))
          )
          )
        }) 
      }) 
    

    JS 代码将工具提示添加到前四列,其值为 hp 列。 这里数据集包含 hp 列,但我们使用 columnDefs 参数隐藏了该列。

    希望对你有帮助!

    【讨论】:

    • 它确实有很大帮助!实际上,这完美地回答了我的问题。非常感谢!
    • @AdamB:rownames=FALSE 只有 4 列,索引为 0、1、2、3。
    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2014-07-05
    • 2016-02-11
    相关资源
    最近更新 更多