Use DataTables smart search on certain columns (using R's DT package)

I want to apply DataTables "smart" search to specific columns, not all columns. Is this functionality possible with R's DT package? Is there a way to add JavaScript that'll get the job done? I'm doing this all in a Shiny app.

I believe I want something similar to the columns().search() method in the DataTables library.

Here's a normal global search of all columns:

library(DT)
library(shiny)
library(shinyWidgets)

dat = data.frame(
  first  = letters[1:3],
  second = letters[4:6],
  third  = letters[7:9]
)
ui = function(request) {
  shiny::fluidPage(
    shiny::sidebarLayout(
      shiny::sidebarPanel(
        shinyWidgets::searchInput(
          inputId = "search_all",
          label = "Search all columns"
        )
      ),
      shiny::mainPanel(DT::dataTableOutput("dt"))
    )
  )
}
server = function(input, output, session) {
  output$dt = DT::renderDataTable({
    dat %>%
      DT::datatable(
        options = list(
          dom = "lrtip",
          search = list(search = input$search_all),
          searchHighlight = TRUE
        )
      )
  })
}
shiny::shinyApp(ui, server)

Here's what I wish I could do, set target columns for each search input (this doesn't work, I'm just including it for illustration):

library(DT)
library(shiny)
library(shinyWidgets)

dat = data.frame(
  first  = letters[1:3],
  second = letters[4:6],
  third  = letters[7:9]
)
ui = function(request) {
  shiny::fluidPage(
    shiny::sidebarLayout(
      shiny::sidebarPanel(
        shinyWidgets::searchInput(
          inputId = "search_1",
          label = "Search 1 column"
        ),
        shinyWidgets::searchInput(
          inputId = "search_2",
          label = "Search 2 columns"
        )
      ),
      shiny::mainPanel(DT::dataTableOutput("dt"))
    )
  )
}
server = function(input, output, session) {
  output$dt = DT::renderDataTable({
    dat %>%
      DT::datatable(
        options = list(
          dom = "lrtip",
          search = list(
            list(
              targets = c("first"),
              search = input$search_1
            ),
            list(
              targets = c("second", "third"),
              search = input$search_2
            )
          ),
          searchHighlight = TRUE
        )
      )
  })
}
shiny::shinyApp(ui, server)

Please note: The r DT::datatable(dat, filter = "top", ...) is not the solution. It does not perform "smart" search.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.