needRhelp
needRhelp

Reputation: 3148

Shiny: Combination of selectizeInput and textInput possible?

I want to have a textInput in a shiny app, which shows the user possible autocompletions from a predefined list (or vector), when the user types sth. So the user can click on this and save time, but has also the possibility to write a text not in the predefined list (so similar to the way Google Search works).

Here is an example. A user could type in "Ap" and can then click on "Apple". But it should also be possible to type in new inputs that are not in the predifined list, e.g. "Orange".

Shinysky is similar to what I want (autocompletion in the textInput, but seems not to allow for new texts not in the choices vector.

library(shiny)
library(shinysky) # install from github
choices = c("Apple", "Banana", "Strawberry")

ui <- fluidPage(
  textInput.typeahead(
    id = "fruit", placeholder="type a fruit name", 
    local = data.frame(fruit = choices, info = c("info1", "info2", "info3")), 
    valueKey = "fruit", 
    tokens = c(1, 2, 3), 
    template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{fruit}}</p> <p class='repo-description'>You need to learn more CSS to customize this further</p>")
    ),
  verbatimTextOutput("value")
)

 server <- function(input, output, session) {
   # typeahead
  observe({
    input$fruit
    showshinyalert(session, "shinyalert3", sprintf("Typeahead Text Input Value: '%s'", 
                                               input$fruit), "error")
  })
  output$value <- renderText({input$fruit})
}

shinyApp(ui, server)

Maybe someone has an idea?

Upvotes: 9

Views: 1961

Answers (1)

needRhelp
needRhelp

Reputation: 3148

Took me nearly two years until I found the solution:

selectizeInput(..., options = list(create = TRUE))

Also mentioned here: shiny - looking for shortcut to combine selectize and textInput

Upvotes: 15

Related Questions