Search code examples
rsearchdatatablesdt

DT::datatable regular expression OR-operator problem


OR-operator works in the first case but not in the second case

This code creates DT::datatable() which returns all rows which match either "mazda" or "datsun"

library(DT)

datatable(
  mtcars, 
  filter = list(position = 'none', clear = TRUE),
  options = list(
    search = list(regex = TRUE, caseInsensitive = TRUE, search = 'mazda|datsun'),
    pageLength = 5
  )
)

So I would expect this code to return all rows which match "good" or "ideal". But it matches only "ideal":

datatable(
  ggplot2::diamonds |> head(),
  filter = list(position = 'none', clear = TRUE),
  options = list(
    search = list(regex = TRUE, caseInsensitive = TRUE, search = 'ideal|good'),
    pageLength = 5
  )
)

What could be wrong here?


Solution

  • You need to disable the smart option (for whatever obscure reason it works for the mtcars, but as per the docs you should always disable smart when using regex):

    library(dplyr)
    library(DT)
    
    datatable(
      ggplot2::diamonds |>
        group_by(cut) |>
        slice_sample(n = 2),
      filter = list(position = 'none', clear = TRUE),
      options = list(
        search = list(regex = TRUE, caseInsensitive = TRUE, search = 'ideal|good', 
                      smart = FALSE), ## disable the smart option
        pageLength = 6
      )
    )
    

    A datatable with 6 rows with a search term "ideal|good" in the top right search box and the rows show the filteres result