Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny: Combination of selectizeInput and textInput possible?

Tags:

r

shiny

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?

like image 458
needRhelp Avatar asked Jan 31 '17 20:01

needRhelp


1 Answers

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

like image 187
needRhelp Avatar answered Nov 09 '22 18:11

needRhelp