Kyle V
Kyle V

Reputation: 43

How to change the column filter control in R Shiny DataTables

The documentation for the R shiny DataTables package (https://rstudio.github.io/DT/ see section 2.8) says this about column filters:

Depending on the type of a column, the filter control can be different. Initially, you see search boxes for all columns. When you click the search boxes, you may see different controls:

For numeric/date/time columns, range sliders are used to filter rows within ranges;

For factor columns, selectize inputs are used to display all possible categories, and you can select multiple categories there (note you can also type in the box to search in all categories);

For character columns, ordinary search boxes are used to match the values you typed in the boxes;

Does DataTables have a way to change this? I have numeric columns but I'd like to filter them using an ordinary search box, not the range sliders.

Upvotes: 4

Views: 2248

Answers (2)

Yasin Amini
Yasin Amini

Reputation: 255

Unfortunately there is no way to be able to control this feature for now. You can change all the columns to factor :

renderedTable <- reactive({

 data.frame(lapply(df,as.factor))  
    
  })

In this way you still be able to choose and search multiple options of numbers but the object are serialized as JSON array without slider.

Upvotes: 0

Gregor de Cillia
Gregor de Cillia

Reputation: 7695

I honestly don't thik there is much you can do if the DT package doesn't support this feature. You could either

  1. Change the column type from numeric to an ordered factor (dat$col <- factor(dat$col, ordered = TRUE))
  2. Submit an issue/PR to the github repo and ask/implement an extension for the filtering interface.
  3. Implement your own filtering UI based on this article.

Maybe take a look at this function to get a better idea how to implement option 3.

Upvotes: 2

Related Questions