Filter brilliant data in multiple conditions

Below is a direct replication of the datatable demo featured on rstudio's shiny website . It is easy to filter a dataset (ideal for diamond or setis on the iris, for example), however is there a way to filter a lot of data like Ideal and Fair in a diamond dataset? I've tried basic "AND" and "&". syntax, spaces, nothing works. It looks like it should be possible, but is it possible or is some kind of circular motion approach required?

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 searching, I suspect I should be using 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 can't seem to get the columnFilter plugin to work. The columnDefs statement (commented out) works fine, but when I try to execute the columnFilter statement, I only get the table header and filter fields. I suspect some syntax must be turned off to get this to work. For an example of functionality, please see this site . Note that this also uses the most recent shiny rstudio github version

+3


source to share


1 answer


Turn off regex acceleration

By default, DataTables avoids regex appearing in search terms. However, since DataTables 1.10 has the option to turn off escaping and allow regex searches. We can use options

to pass options datatable

, for example:

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

      

Your bindings can now use regular expressions. For example, to filter a table for Mazda or Chrysler, you can search Mazda|Chrysler

.



Here's the official RStudio page on the matter.

Sample application

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)

      

0


source







All Articles