简体   繁体   English

如何指定行/列以应用R Shiny的回调函数?

[英]How to specify the Rows/Columns to apply R Shiny's Callback functions?

I'd like to specify the columns/rows that I want to apply my callback function (In short, I'm trying to add a tooltip only for specific cells in my Datatable). 我想指定要应用回调函数的列/行(简而言之,我试图仅针对数据表中的特定单元格添加工具提示)。 Here's a part of the code where I'm trying to address this issue: 这是我尝试解决此问题的代码的一部分:

output[[(paste("table_", name, sep=""))]] <- DT::renderDataTable({
  DT::datatable(content[[name]]$data,
  container = sketch,
  rownames = FALSE,
  filter = "top",
  options = list(
    columnDefs = list(list(
      targets = rules_indexes[[name]]-1, #I would like to specify this for the callback below
      render = JS(
        "function(data, type, row, meta) {",
        "return type === 'display' && ",
        "'<span title=\"' + 'span' + '\">' + data +'</span>';",
        "}") #this approach doesn't work when the elements have no content (nothing to hover)
    ))

),
callback = JS("
      table.on('mouseenter', 'tbody td', function() {
        var column = $(this).index();
        var row = $(this).parent().index();
        if ( row==0){
        this.setAttribute('title', 'callback');}

      });

      return table;
      ") #this approach works but I don't know how to specify the rows/columns to apply this callback to.
)%>% 
    formatStyle(columns = rules_indexes[[name]],backgroundColor = styleEqual("","crimson"))
})

} }

I'm trying to add a tooltip only for specific cells in my Datatable 我正在尝试仅针对数据表中的特定单元格添加工具提示

You can do like this: 您可以这样:

library(DT)

# we will place a tooltip at cell(1,1) and at cell(3,2)
rows <- "[1,3]"
cols <- "[1,2]"
tooltips <- "['foo','bar']"

datatable(head(iris), 
          options=list(initComplete = JS(c(
            "function(settings){",
            sprintf("  var rows = %s;", rows),
            sprintf("  var cols = %s;", cols),
            sprintf("  var tooltips = %s;", tooltips),
            "  var table = settings.oInstance.api();",
            "  for(var i=0; i<rows.length; i++){",
            "    var cell = table.cell(rows[i],cols[i]);",
            "    cell.node().setAttribute('title', tooltips[i]);",
            "  }",
            "}")))
)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM