DT闪亮R工具提示和隐藏列

时间:2016-10-24 14:06:43

标签: javascript css r shiny dt

我正试图在闪亮的R中的DT数据表上做两件事。

我的代码(来自github的示例)如下:

library("shiny")
library("shinydashboard")
library("datasets")
library("DT")

header <- dashboardHeader()

sidebar <- dashboardSidebar()

body <- dashboardBody(
  DT::dataTableOutput("mtcarsTable")
)

shinyApp(
  ui = dashboardPage(header, sidebar, body),
  server = function(input, output) {

    output$mtcarsTable <- renderDataTable({
      DT::datatable(datasets::mtcars, 
                    options = list(rowCallback = JS('
                                                    function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                                                    // Bold and green cells for conditions
                                                    if (parseFloat(aData[4]) >= 200)
                                                    $("td:eq(3)", nRow).css("font-weight", "bold");
                                                    if (parseFloat(aData[4]) >= 100)
                                                    $("td:eq(3)", nRow).css("background-color", "#9BF59B");
                                                    }')
                      )
                    )
  })
  }
                    )

正如您所看到的,我正在评估第4列,为单元格提供背景颜色,以及定义它是否应该是粗体。

是否可以隐藏第4列?我只想评估它,我不希望它被显示出来。

我的另一个问题是,是否可以仅向具有绿色背景的单元格添加工具提示?我看到我应该使用回调,但我不知道如何,我不是javascript的专家。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

是的,可以仅向具有绿色背景的单元格添加工具提示。我们必须使用下面的javascript:

DT::datatable(datasets::mtcars, 
              options = list(rowCallback = JS(
                "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                "// Bold and green cells for conditions",
                "if (parseFloat(aData[4]) >= 200)",
                "$('td:eq(3)', nRow).css('font-weight', 'bold');",
                "if (parseFloat(aData[4]) >= 100){",
                "$('td:eq(3)', nRow).css('background-color', '#9BF59B');",
                "var full_text = aData[3]",
                "$('td:eq(3)', nRow).attr('title', full_text);",
                "}",
                "}")
              )
)

<强> [编辑]:

要在工具提示中添加格式,我添加了更多行,它只适用于shinyApp而不是DT数据。请参阅以下代码:

header <- dashboardHeader()

sidebar <- dashboardSidebar()

body <- dashboardBody(
  DT::dataTableOutput("mtcarsTable")
)

shinyApp(
  ui = dashboardPage(header, sidebar, body),
  server = function(input, output) {

    output$mtcarsTable <- renderDataTable({
      DT::datatable(datasets::mtcars, 
                    options = list(rowCallback = JS(
                      "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                      "// Bold and green cells for conditions",
                      "if (parseFloat(aData[4]) >= 200)",
                      "$('td:eq(3)', nRow).css('font-weight', 'bold');",
                      "if (parseFloat(aData[4]) >= 100){",
                      "$('td:eq(3)', nRow).css('background-color', '#9BF59B');",
                      "var full_text = aData[3]",
                      "$('td:eq(3)', nRow).attr('title', full_text);",
                      "//Code for formatting tooltip",
                      "$('td:eq(3)', nRow).tooltip({",
                        "'delay': 0,",
                        "'track': true,",
                        "'fade': 250,",
                      "});",


                      "}",
                      "}")
                    )
      )

    })
  }
)

希望这有帮助。

相关问题