Regular Expression Searching within Shiny Selectize Objects

[This article was first published on Rstats on Reproducible Random Thoughts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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(<selectizeInput Choices>))
# ui side: 
  regexSelectUI(id = "myId", label = 'myLabel', choices = <selectizeInput 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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)