Skip to content Skip to sidebar Skip to footer

Dt Shiny R Tooltip And Hide Column

I'm trying to do two things on a DT data table in shiny R. My code (example from github) is the following: library('shiny') library('shinydashboard') library('datasets') library('D

Solution 1:

Yes it is possible to add a tool tip only to the cells with green background. We have to use javascript below:

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

[EDIT]:

To add formattings to the tooltip I have added few more lines and it only works in shinyApp and not DT datatable. See the code below:

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


                      "}",
                      "}")
                    )
      )

    })
  }
)

Hope this helps.

Post a Comment for "Dt Shiny R Tooltip And Hide Column"