Use of regular expressions in selectizeInput in Shiny app

I am using selectizeInput in Shiny and would like this option to show only those options that start with the characters that the user entered in the search field. By default selectizeInput gives all options that contain the string entered by the user, regardless where that string appears in the option strings.

In the documentation of selectize.js the option CreateFilter looks promising, so I tried to use this in the following example:

library("shiny")

selectList <- sapply(1:10000, function(x) paste(sample(letters, 8), collapse = ''))

ui <- fluidPage(
  selectizeInput(inputId = 'mylist', 
                 label = 'Select Something', 
                 choices = NULL, 
                 selected = 1)
)

server <- function(input, output, session) 
{
  updateSelectizeInput(session = session, 
                       inputId = 'mylist', 
                       choices = c(Choose = '', selectList), 

                       options = list(createFilter = 
                                      grep(paste0("^", input$mylist, "."),
                                      selectList, 
                                      value=TRUE)),
                       server = TRUE)
}

shinyApp(ui = ui, server = server)

but the regular expression that is given a parameter of 'createFilter' does not work the way I had hoped for. I was also struggling how to refer in the expression to the character (just) entered by the user in the search field (without having chosen an option yet). I tried 'input$mylist', but this is evidently wrong.

Does anyone know how to make this work?

A proposal with JavaScript:

library("shiny")

selectList <- sapply(1:10000, function(x) paste(sample(letters, 8), collapse = ''))

ui <- fluidPage(
  selectizeInput(inputId = 'mylist', label = 'Select something', choices = NULL, selected = 1),
  actionButton("search", "search"), br(), br(),
  textOutput("value")
)

server <- function(input, output, session) {
   updateSelectizeInput(session = session, inputId = 'mylist', choices = c(Choose = '', selectList), server = TRUE,
                       options = list(placeholder = "Select something", 
                                      dropdownParent = 'body', 
                                      openOnFocus = FALSE,
                                      items = c(),
                                      score = I("function(search) 
                                                 {
                                                   var score = this.getScoreFunction(search);
                                                   return function(item) 
                                                   {
                                                     return item.text
                                                     .toLowerCase()
                                                     .startsWith(search.toLowerCase()) ? 1 : 0;
                                                   };
                                                 }")
                       )
  )

  getValue <- eventReactive(input$search, { return(input$mylist) })
  output$value <- renderPrint({ return(getValue()) })
}

shinyApp(ui = ui, server = server)

but it does not work yet. Any ideas?

1 Like

Your solution actually seems to work, if you replace return item.text with return item.label.

1 Like

Thanks!!!
Is it ok if I post your answer also at:

mentioning your name?

Hi @heeringa! Since you waited several days before cross-posting this to SO, I think that falls into the realm of accepted cross-posting strategies. I would recommend that in the future if you take this route on a question that has not been answered, to please just post a link to the cross-post so that future would-be answer-ers don't spend time trying to solve a question that has already been answered.

I would recommend posting the same to SO.

Here is the related FAQ for the topic: FAQ: Is it OK if I cross-post?