自动完成并选择文本框中的多个值闪亮

Eka*_*Eka 13 textbox r autocomplete shiny

是否可以使用类似于Google搜索的自动完整字符串和闪亮文本框中的堆栈溢出标记选择来选择多个值.

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

我想从上面的数据集中选择多个变量作为我的文本框中的自动填充并将其传递给我的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"))

    )
  )
))
Run Code Online (Sandbox Code Playgroud)

server.R

# server.R

shinyServer(function(input, output) {

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

 })


}
)
Run Code Online (Sandbox Code Playgroud)

EDITED

我已经使用shinysky的select2input来选择多个变量,但现在我已经添加了提交按钮来一起获取所选值.

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

actionButton("go","submit") 
Run Code Online (Sandbox Code Playgroud)

我想绑定选定的选项让我们说用户选择a和c然后新的变量是

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

但这不起作用

Por*_*hop 16

看看shinysky包装和textInput.typeahead.您可以进一步自定义textinput自己的风格.编辑:select2Inputshinysky包中添加了示例也供参考

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

根据请求编辑2.请reactive像我一样将对象包装在表达式中,var <- reactive({...})以便稍后重新使用它们

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)
Run Code Online (Sandbox Code Playgroud)


and*_*har 6

恕我直言,一个更简单的方法是使用shiny::selectizeInput(). 它允许您通过选择参数自动完成输入。

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)
Run Code Online (Sandbox Code Playgroud)