stianlagstad
stianlagstad

Reputation: 3302

R and DT: Show filter option on specific columns

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.

Upvotes: 8

Views: 9355

Answers (1)

Claudiu Papasteri
Claudiu Papasteri

Reputation: 2609

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))
  )
)

Upvotes: 6

Related Questions