3

My issue is with the global search bar in the datatable package.

I'm trying to set this global search bar to find an exact match. There're some java query based instructions here:

search exact match and highlight jquery datatable regex

However, I need to know how to do the same thing in Rstudio. I think I have to use JS() function, but, I'm not sure how to do it.

I came up with the "smart=FALSE" solution but it doesn't work for me.

Also, I can't use shiny app as we don't have it on my company's server.

Here is the dummy example:

library(DT)
f=data.frame(c(10,12,35),c('aa','ab','cc'))
colnames(f)<- c('Col1','Col2')
datatable(f)

If you type 'a' in the global search bar, you'll get 2 observations, where both includes 'a'. However, I would like to get no observation as there's no exact match for 'a' in the table.

Any help is highly appreciated.

Regards,

Adel

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
Adel
  • 41
  • 5

1 Answers1

2

This works only in a browser, not in the RStudio viewer:

js <- c(
  "function(settings){",
  "  var instance = settings.oInstance;",
  "  var table = instance.api();",
  "  var input = instance.parent().find('.dataTables_filter input');",
  "  input.off('keyup search input').on('keyup', function(){",
  "    var keyword = '\\\\b' + input.val() + '\\\\b';",
  "    table.search(keyword, true, false).draw();",
  "  });",
  "}"
)

datatable(iris[c(1,2,51,52,101,102),], 
          options = list(initComplete = JS(js))
)

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225