0

I have an R Shiny app like this:

library(shiny)
library(DT)

# Example data
data <- data.frame(col1 = c("syncope", "fall", "dizziness", "headache", "back pain"),
                   col2 = c(1, 2, 3, 4, 5))

# Shiny app
ui <- fluidPage(
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  output$mytable <- DT::renderDataTable({
    DT::datatable(data, filter = 'top',options = list(search = list(regex = TRUE)))
  })
}

shinyApp(ui, server)

I was able to code it to this way to allow filtering with multiple conditions, thanks to this question. However, Regex isn't really that intuitive. Someone would have to enter sync|dizz to filter down to the rows that show syncope and dizziness. Or p[ae] to filter on back pain and syncope.

Is it possible to code things in such a way that you could separate 2 strings with a space, or with the word 'OR', to achieve this instead? For instance, you could enter sync dizz, or maybe sync OR dizz. This would be more intuitive for users.

Please note that I am referring to the search boxes that are above each column (blue arrow), not the general Search box that was referred to in a similar question (red arrow).

enter image description here

P E
  • 165
  • 11

1 Answers1

0

Here is an alternative approach to the excellent answer of @Stéphane Laurent that could be an option for you:

The only change to your code is to transform the column of interest to factor: data$col1 <- as.factor(data$col1) The you will get a dropdown list, also typing and hitting enter for more entries is possible:

library(shiny)
library(DT)

# Example data
data <- data.frame(col1 = c("syncope", "fall", "dizziness", "headache", "back pain"),
                   col2 = c(1, 2, 3, 4, 5))

data$col1 <- as.factor(data$col1)

# Shiny app
ui <- fluidPage(
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  output$mytable <- DT::renderDataTable({
    DT::datatable(data, filter = 'top',options = list(search = list(regex = TRUE)))
  })
}

shinyApp(ui, server)

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66