Skip to content
Advertisement

How to filter (search) formatted columns in R DT (DataTable)

When applying formatting to a column in DT::datatable(), the DataTables automatic column filter is not working. For example:

JavaScript

The filter for column b fails.

I think this is a common enough problem that there has to be a solution. I have been searching, but without success so far. Would be grateful if someone points me to where this has been solved.

I am aware that by using the JS callback function on the DT website (https://rstudio.github.io/DT/options.html see 4.5 Row rendering and run it with filter="top"), one can filter with formatted columns. But I am trying to implement a general solution for an R package, so I was hoping for an R solution.

Workaround

I have an approach that works in some cases. The approach is to copy the raw numbers column to a new column that is hidden on rendering. But ‘link’ the formatted numbers (column) to the raw numbers column (values_column) in formatStyle. Then, luckily, and not exactly sure why, searching on the formatted column works. But only in the case of suffixes (like %), but not for prefixes ($) or big number formatting with commas for thousands.

Code for each of these cases is below.

I don’t mind using this workaround, but don’t know enough about the inner workings of DT to make prefixes and other formats work.

Suffix works

JavaScript

Prefix fails

JavaScript

Big number fails

JavaScript

Advertisement

Answer

Using DR::renderDT with server=TRUE (the default) inside a Shiny app solves this problem using the regular DT::format* functions. Outside Shiny context, the examples still don’t work. But that is irrelevant in my case, as the tables will go in a Shiny app.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement