Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R and DT: Show filter option on specific columns

Tags:

r

datatables

dt

I want to use DT to display some data and allow filtering on only some of the columns. This code:

df <- data.frame(c("john","susy"), c("a", "b"))
names(df) <- c("name", "grade")
DT::datatable(df, filter = 'top')

Creates a table with filtering options over each column:

Now say I only want to have the filter box visible for the "name" column. How do I do that? I thought I could use filter like this:

df <- data.frame(c("john","susy"), c("a", "b"))
names(df) <- c("name", "grade")
DT::datatable(df, filter = c('none', 'top'))

To only enable it on the second column, but it doesn't work (it only takes a single character argument). Any ideas? Note that I want the top right search box to search on all fields, but I only want the column-specific box over the second column.

like image 531
L42 Avatar asked Mar 17 '16 13:03

L42


1 Answers

I dont belive you can modify the aspect of the filter option, but you can disable its function by setting searchable = FALSE for the specific columns you don't want to be filtered on.

I hope this is what you are looking for:

DT::datatable(
  df, filter = 'top',
  options = list(
    columnDefs = list(list(targets = 1, searchable = FALSE))
  )
)
like image 54
Claudiu Papasteri Avatar answered Oct 19 '22 18:10

Claudiu Papasteri