By default, searching does not use regular expressions, and is case-insensitive. You can turn on regular expressions by using the option search = list(regex = TRUE)
, and perform case-sensitive searching by list(search = list(caseInsensitive = FALSE))
(the two options are orthogonal).
library(DT)
= mtcars[, c(1:5, 9)]
mtcars2 $am = factor(mtcars$am, c(0, 1), c('automatic', 'manual'))
mtcars2# search for Ma or Me
datatable(
colnames = c('model' = 1),
mtcars2, filter = list(position = 'top', clear = FALSE),
options = list(
search = list(regex = TRUE, caseInsensitive = FALSE, search = 'M[ae]'),
pageLength = 5
) )
Please note the interpretation of regular expressions depends on whether you use client-side or server-side processing. For client-side processing, the search string is treated as JavaScript regular expressions. For server-side processing, it is up to the server-side script. In DT, the server-side processing function is implemented in R, so the search string is treated as R regular expressions.
In the above example, we specified an initial search string M[ae]
(meaning Ma
or Me
) in the global search box. We can also specify initial search strings for individual columns via the searchCols
option when column filters are enabled. The searchCols
option is a list of the same length as the number of columns of the table. Each element in the list is either NULL
or a list of the form list(search = 'keyword')
. In the example below, we search for er
in the first column, filter the second column using [15, 25]
, and filter the last column by "automatic"
.
datatable(
colnames = c('model' = 1),
mtcars2, filter = list(position = 'top', clear = FALSE),
options = list(pageLength = 5, searchCols = list(
list(search = 'er'),
list(search = '15 ... 25'),
NULL, NULL, NULL, NULL,
list(search = '["automatic"]')
)) )