【问题标题】:force selectize.js only to show options that start with user input in Shiny强制 selectize.js 仅显示以 Shiny 中的用户输入开头的选项
【发布时间】:2018-08-27 12:31:10
【问题描述】:

我在 Shiny 中使用 selectizeInput 并希望调整此功能,以便仅以用户在搜索字段中输入的字符开头的单词显示在选项列表中。我在以下位置查看 JavaScript 代码: force selectize.js only to show options that start with user input 并尝试了以下方法:

library("shiny")

selectList <- sapply(1:10000, function(x) paste(sample(letters, 8), collapse = ''))

ui <- fluidPage(
  selectizeInput(inputId = 'mylist', label = 'Select something', choices = NULL, selected = 1),
  actionButton("search", "search"), br(), br(),
  textOutput("value")
)

server <- function(input, output, session) {
   updateSelectizeInput(session = session, inputId = 'mylist', choices = c(Choose = '', selectList), server = TRUE,
                       options = list(placeholder = "Select something", 
                                      dropdownParent = 'body', 
                                      openOnFocus = FALSE,
                                      items = c(),
                                      score = I("function(search) 
                                                 {
                                                   var score = this.getScoreFunction(search);
                                                   return function(item) 
                                                   {
                                                     return item.text
                                                     .toLowerCase()
                                                     .startsWith(search.toLowerCase()) ? 1 : 0;
                                                   };
                                                 }")
                       )
  )

  getValue <- eventReactive(input$search, { return(input$mylist) })
  output$value <- renderPrint({ return(getValue()) })
}

shinyApp(ui = ui, server = server)

但是目前还不行。也许我错过了一些简单的东西,但是有人知道这段代码应该改变什么吗?

【问题讨论】:

  • 您好,Wilbert,感谢您提请我注意。我从来没有和 Shiny 一起工作过,所以,我在这里离开了我的舒适区。但是看代码,将整个 store 函数放在一个函数中并放在 "" 内看起来很奇怪。我的第一感觉是改变它。但话又说回来,我不熟悉语法。有没有机会把它放在 GitHub 上以便在本地进行测试?

标签: javascript shiny selectize.js


【解决方案1】:

解决方案:将 item.text 替换为项目标签(感谢 jcheng,RStudio 社区)。我稍微改了一下代码:

library("shiny")

selectList <- sapply(1:100000, function(x) paste(sample(letters, 8), collapse = ''))

ui <- fluidPage(
  selectizeInput(inputId = 'mylist', label = 'Select something', choices = NULL, selected = 1),
  actionButton("search", "search"), br(), br(),
  textOutput("value")
)

server <- function(input, output, session)
{
  getScore <- function()
  {
    return(I("function(search)
              {
               var score = this.getScoreFunction(search);
               return function(item)
                      {
                        return item.label
                        .toLowerCase()
                        .startsWith(search.toLowerCase()) ? 1 : 0;
                      };
              }"
            )
          )
  }

  updateSelectizeInput(session = session, inputId = 'mylist', choices = c(Choose = '', selectList), server = TRUE,
                       options = list(placeholder = "Select something", dropdownParent = 'body',
                                      openOnFocus = FALSE, items = c(), score = getScore()))

  getValue <- eventReactive(input$search, { return(input$mylist) })
  output$value <- renderPrint({ return(getValue()) })
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-28
    • 2014-07-02
    • 1970-01-01
    • 2018-06-14
    • 2017-10-03
    • 1970-01-01
    • 2020-08-01
    • 2020-02-05
    相关资源
    最近更新 更多