19

Is it possible to select multi values using auto complete strings similar to google search and stack overflow tags selection in shiny text box.

dataset<-cbind("John Doe","Ash","Ajay sharma","Ken Chong","Will Smith","Neo"....etc)

I want to select multiple variables from the above dataset as a auto fill in my textbox and pass it to my server.R

ui.R

shinyUI(fluidPage(
  titlePanel("test"),

  sidebarLayout(
    sidebarPanel(
      helpText("text"),

      textInput("txt","Enter the text",""),
      #Pass the dataset here for auto complete

     ),

    mainPanel(
      tabsetPanel(type="tab",tabPanel("Summary"),textOutput("text2"))

    )
  )
))

server.R

# server.R

shinyServer(function(input, output) {

output$text2<- renderText({
paste("hello",input$txt)

 })


}
)

EDITED

I have used select2input from shinysky for selecting mulitiple varialbes but now I have added submit button to get selected values together.

#ui.R
select2Input("txt","This is a multiple select2Input",choices=c("a","b","c"),selected=c("")),

actionButton("go","submit") 

I want to bind selected option lets say user selected a and c then new variable is

#server.R
input$go #if pressed submit button
var<-cbind("a","c")
output$text<-renderText({ print ("var")})

but this is not working

Eka
  • 14,170
  • 38
  • 128
  • 212
  • 3
    Your edit is an elementary question, you should read up on `shiny` as these things are very simple to do. I will edit my question again, but next time post a new question – Pork Chop Feb 08 '16 at 12:16

2 Answers2

20

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)

enter image description here

enter image description here

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)
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • 1
    hey thanks for pointing to shinysky.. In shinysky this function `select2Input()` is more appropriate for my code.. Once more thanks – Eka Feb 08 '16 at 10:20
  • I have added additonal doubts to my question would you be able to help me – Eka Feb 08 '16 at 11:35
  • thank you for this helpful answer. Please, allow me one follow-up question. My vector of values has length 50,000 (imagine you have a list of cities available in your DB). This causes shiny to take a long long time to load. What can I do in this case? – Angelo Feb 22 '22 at 16:01
10

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)
andschar
  • 3,504
  • 2
  • 27
  • 35
  • selectizeInput doesn't allow for repeated values. – Saren Tasciyan Jun 25 '20 at 09:23
  • thank you for this helpful answer. Please, allow me one follow-up question. My vector of values has length 50,000 (imagine you have a list of cities available in your DB). This causes shiny to take a long long time to load. What can I do in this case? – Angelo Feb 22 '22 at 16:01
  • @Angelo It's been a while since I worked with shiny and can't help you further. I suggest to simply ask a question here on SO. – andschar Feb 22 '22 at 16:13