Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing how DataTables displays missing values in Shiny [duplicate]

DataTables, in Shiny, displays missing values as blank space. Is there a way to change that? I'm dreaming especially of the gray, italicized NAs that RStudio uses in its data viewer. I'd have no problem with injecting such strings into character columns for display purposes, but, of course, sometimes the columns are numeric, or date, and converting them just for display seems problematic.

MWE of DT's default missing values display:

library(DT)
library(shiny)

ui <- fluidPage(
    dataTableOutput("airquality")
)

server <- function(input, output) {
    output$airquality <- renderDataTable(airquality)
}

shinyApp(ui = ui, server = server)

default_dt_display

like image 893
DHW Avatar asked Oct 23 '19 15:10

DHW


2 Answers

You can do:

library(DT)

rowCallback <- c(
    "function(row, data){",
    "  for(var i=0; i<data.length; i++){",
    "    if(data[i] === null){",
    "      $('td:eq('+i+')', row).html('NA')",
    "        .css({'color': 'rgb(151,151,151)', 'font-style': 'italic'});",
    "    }",
    "  }",
    "}"  
  )

datatable(airquality, options = list(rowCallback = JS(rowCallback)))

enter image description here

like image 189
Stéphane Laurent Avatar answered Nov 05 '22 15:11

Stéphane Laurent


Customizing one of the options might be a good start:

options(htmlwidgets.TOJSON_ARGS = list(na = 'string'))

Here's the source so you can read the complete thread about this issue and its workarounds.

like image 32
Sorlac Avatar answered Nov 05 '22 15:11

Sorlac