我在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)
,但尚不可用。也许我错过了一些简单的事情,但是有人知道该代码中应该更改什么吗?
答案 0 :(得分:0)
解决方案:用项标签替换item.text(感谢RStudio社区jcheng)。我对代码进行了一些重组:
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)