Regular Expression Searching within Shiny Selectize Objects

September 25, 2017

(This article was first published on Rstats on Reproducible Random Thoughts, and kindly contributed to R-bloggers)

regexSelect is a small package that uses Shiny modules to solve a problem in Shiny selectize objects – regular expression (regex) searching. You can quickly filter the values in the selectize object, while being able to add that new regex query to the selectize list.

This is great for long lists, since you can return multiple item simultaneously without needing to endlessly click items in a list!



Below are two examples of using regular expressions to quickly populate multiple items in a ggplot and a datatable.

regexSelect with Plots

The shiny module works with two main functions:

# server side: 
  callModule(module=regexSelect, id='myId', reactive())
# ui side: 
  regexSelectUI(id = "myId", label = 'myLabel', choices = )

regexSelect comes with controls placed in a group checkbox below the selectize object. When calling regexSelect you can show or hide the checkbox controls (hidden by default), as to make it look like a normal selectize object, and save valuable UI real estate.

While the shiny app is running regexSelect properties can be manipulated through the checkbox controls giving greater flexibilty to:
  - Toggle regexSelect to work as a standard selectize object.
  - Retain the regex search as a new value the selectize object.
  - Change arguments that are passed to [base::grep]( : ``, `perl`, `fixed`, `invert`.

ui <- fluidPage(

              'Choose Variable',
              choices = names(diamonds)[sapply(diamonds,function(x){
              selected = 'clarity'),
server <- function(input, output, session) {
  regexSelectUI(id = "a", 
                label = input$var,
                choices = unique(diamonds[[input$var]]), 
       = TRUE)
    curr_cols <- callModule(module = regexSelect, 
                            id = "a",
      cols_now <- curr_cols()
      output$data <- shiny::renderPlot({
shinyApp(ui, server)

regexSelect with Tables

ui <- shiny::fluidPage(

  regexSelectUI(id = "a", 
                label = "Variable:",
                choices = names(iris)


server <- function(input, output, session) {

  curr_cols <- callModule(module = regexSelect, 
                          id = "a",
    cols_now <- curr_cols()
    if( length(cols_now)==0 ) 
        cols_now <- names(iris)
    output$data <- shiny::renderTable({
      iris[,cols_now , drop = FALSE]
    }, rownames = TRUE)

shiny::shinyApp(ui, server)

To leave a comment for the author, please follow the link and comment on their blog: Rstats on Reproducible Random Thoughts. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)