javascriptcssrshinydt

How to hide a column and set up a tooltip for certain cells?


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

As you can see I'm evaluating column 4, to give to a cell a background color as also as to define if it should be bold or not.

Is it possible to hide column 4? I just want to evaluate it, I don't want it to be shown.

My other question is if it is possible to add a tooltip only to the cells with green background? I saw that I should use the callback but I don't know how and I'm not an expert at javascript.


Solution

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