首页 文章

强制selectize.js仅显示以Shiny中的用户输入开头的选项

提问于
浏览
0

我在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)

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

1 回答

  • 0

    解决方案:按项目标签替换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)
    

相关问题