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?