5

The following is a direct replication of the datatable demo provided on the rstudio shiny website. It is quite easy to filter the dataset (e.g. Ideal on the diamond, or setosa on the iris), however is there a way to filter multiple conditions such as 'Ideal' and 'Fair' in the diamond dataset? I have tried the basic 'AND' and '&' syntax, spaces, nothing seems to work. This seems like it should be possible but is this even possible or does it require some roundabout approach?

require(shiny)
runApp(
  list(ui = fluidPage(
    title = 'Examples of DataTables',
    sidebarLayout(
      sidebarPanel(
        conditionalPanel(
          'input.dataset === "diamonds"',
          checkboxGroupInput('show_vars', 'Columns in diamonds to show:',
                             names(diamonds), selected = names(diamonds))
        )
      ),
      mainPanel(
        tabsetPanel(
          id = 'dataset',
          tabPanel('diamonds', dataTableOutput('mytable1'))
        )
      )
    )
  ),
  server = shinyServer(function(input, output) {

    # a large table, reative to input$show_vars
    output$mytable1 <- renderDataTable({
      library(ggplot2)
      diamonds[, input$show_vars, drop = FALSE]
    })

  })
  )
)

After some further search, I suspect I should be able to use the jquery column filter plugin. To simplify this question, here is a more stripped down version of the above code:

library(shiny)
runApp(
  list(ui = basicPage(
    h1('Diamonds DataTable with TableTools'),

    # added column filter plugin
    singleton(tags$head(tags$script(src='https://code.google.com/p/jquery-datatables-column-filter/source/browse/trunk/media/js/jquery.dataTables.columnFilter.js',
                                    type='text/javascript'))),
    dataTableOutput("mytable")
  )
  ,server = function(input, output) {
    output$mytable = renderDataTable({
      diamonds[,1:6]
    }, options = list(
      pageLength = 10,
      columnDefs = I('[{"targets": [0,1],
                     "searchable": true}]')
    )
    )
  }
))

However, I cannot seem to get the columnFilter plugin to work. The columnDefs statement (commented out) works fine but when I try to do the columnFilter statement, I get only get the table header and filter search boxes. I suspect some syntax must be off to get this to work. As an example of the functionality, please see this website. Please note, this is also using the most recent version of shiny from the rstudio github

cdeterman
  • 19,630
  • 7
  • 76
  • 100
  • filter from just running web interface? – xiaodai Sep 01 '14 at 05:52
  • @xiaodai, yes, when the web interface is running, I could type Ideal to filter the diamond dataset to only 'Ideal' rows but cannot figure out how to filter 'Ideal' and 'Fair' as an example. How could one select multiple groups like this? – cdeterman Sep 02 '14 at 00:55

1 Answers1

7

Turn off regex escaping

By default, DataTables escapes regex characters in search terms. However, since DataTables 1.10, there's an option to disable the escaping and allow regex searches. We can use options to pass the option to datatable, like this:

library(DT)
datatable(mtcars, 
          options = list(search = list(regex = TRUE)))

Now your seaches can use regular expressions. For example, to filter the table for Mazda or Chrysler, you could search Mazda|Chrysler.

Here's the official RStudio page on the matter.

Example app

enter image description here

library(shiny)
library(DT)

ui <- fluidPage(
    fluidRow(
        column(width = 9,
               br(),
               DT::dataTableOutput("dt")
               ),
        column(width = 3,
               br(),
               radioButtons("regex", "Enable Regex?", choices = c("yes", "no"), inline = T))
    )
)

server <- function(input, output, session) {
    output$dt <- DT::renderDataTable({
        value <- ifelse(input$regex == "yes", TRUE, FALSE)
        datatable(mtcars, 
                  options = list(search = list(regex = value))
        )
    })
}

shinyApp(ui, server)
Hallie Swan
  • 2,714
  • 1
  • 15
  • 23