Auto complete and selection of multiple values in text box shiny
Look into shinysky
package and textInput.typeahead
. You can further customize the style of the textinput
yourself. Edit: I added example with select2Input
from the shinysky
package also for reference
rm(list = ls())
library(shinysky)
library(shiny)
my_autocomplete_list <- c("John Doe","Ash","Ajay sharma","Ken Chong","Will Smith","Neo")
ui <- shinyUI(
fluidPage(tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }"),
tags$style(type="text/css","#search { top: 50% !important;left: 50% !important;margin-top: -100px !important;margin-left: -250px
!important; color: blue;font-size: 20px;font-style: italic;}"),
mainPanel(
# one way of doing it
textInput.typeahead(id="search",
placeholder="Type your name please",
local=data.frame(name=c(my_autocomplete_list)),
valueKey = "name",
tokens=c(1:length(my_autocomplete_list)),
template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{name}}</p>")
),
br(),br(),
# using select2Input
select2Input("select2Input1","",choices=c(my_autocomplete_list),type = c("input", "select"))
)
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
Edit 2 as per request. Please wrap your objects in a reactive
expressions as I did e.g. var <- reactive({...})
so you can re-use those later
rm(list = ls())
library(shinysky)
library(shiny)
my_autocomplete_list <- c("John Doe","Ash","Ajay sharma","Ken Chong","Will Smith","Neo")
ui <- shinyUI(
fluidPage(sidebarPanel(select2Input("txt","",choices=c("a","b","c"),selected=c("")), br(),actionButton("go","submit"), width =2),
mainPanel(textOutput('text'))
)
)
server <- function(input, output, session) {
var <- reactive({
if(input$go==0){return()}
isolate({
input$go
cbind("a","c")
})
})
output$text <- renderText({var()})
}
shinyApp(ui = ui, server = server)
A much easier approach imho is to use shiny::selectizeInput()
. It allows you to autocomplete inputs with via the choices argument.
rm(list = ls())
library(shiny)
my_autocomplete_list <- c("John Doe","Ash","Ajay sharma",
"Ken Chong","Will Smith","Neo")
ui <- shinyUI(
selectizeInput(
inputId = 'search',
label = 'Search',
choices = my_autocomplete_list,
selected = NULL,
multiple = TRUE, # allow for multiple inputs
options = list(create = FALSE) # if TRUE, allows newly created inputs
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)