【问题标题】:Auto complete and selection of multiple values in text box shiny自动完成并在文本框中选择多个值
【发布时间】:2016-02-08 09:15:01
【问题描述】:

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

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

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

    )
  )
))

服务器.R

# server.R

shinyServer(function(input, output) {

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

 })


}
)

已编辑

我使用了来自 shinysky 的 select2input 来选择多个变量,但现在我添加了提交按钮来将选定的值放在一起。

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

actionButton("go","submit") 

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

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

但这不起作用

【问题讨论】:

  • 您的编辑是一个基本问题,您应该阅读shiny,因为这些事情非常简单。我将再次编辑我的问题,但下次发布一个新问题

标签: r autocomplete textbox shiny


【解决方案1】:

查看shinysky 包和textInput.typeahead。您可以自己进一步自定义textinput 的样式。 编辑:我添加了来自shinysky 包的select2Input 示例,也供参考

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)

根据要求编辑 2。请像我一样将您的对象包装在 reactive 表达式中,例如var &lt;- 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)

【讨论】:

  • 嘿,感谢您指向 shinysky。在 shinysky 中,此函数 select2Input() 更适合我的代码。再次感谢
  • 我在我的问题中添加了额外的疑问,你能帮我吗
  • 感谢您提供这个有用的答案。请允许我提出一个后续问题。我的值向量长度为​​ 50,000(假设您的数据库中有一个可用城市列表)。这会导致闪亮需要很长时间才能加载。在这种情况下我该怎么办?
【解决方案2】:

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

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)

【讨论】:

  • selectizeInput 不允许重复值。
  • 感谢您提供这个有用的答案。请允许我提出一个后续问题。我的值向量长度为​​ 50,000(假设您的数据库中有一个可用城市列表)。这会导致闪亮需要很长时间才能加载。在这种情况下我该怎么办?
  • @Angelo 我与闪亮合作已经有一段时间了,无法进一步帮助您。我建议在这里简单地问一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 2013-10-12
  • 2015-11-27
  • 2014-12-15
  • 2012-10-28
相关资源
最近更新 更多