英文:
R shiny selectInput - preselect list begins only with the letters written in inputfield
问题
抱歉,由于您要求只返回翻译好的部分并且不回答问题,以下是您提供的文本的翻译部分:
Hei,
我有一个非常长的标题列表(> 1000个项目)。我想在R闪亮应用程序中使用`selectInput`选择其中一个。在选择一个项目之前,我想在`selectInput`中键入字母以缩短标题列表。这是`selectInput`的通常行为。
我的目标是,只有以输入到`selectInput`输入字段中的字母开头的项目才会显示在预选列表中。这样,用户可以像在字典中一样轻松地使用以特定字母开头的标题。
例如,我的标题是:
headers <- c(
"Apple", "Banana", "Carrot", "Date", "Eggplant", "Fig", "Grape",
"Honeydew", "Iceberg lettuce", "Jackfruit", "Kiwi", "Lemon",
"Ananas", "Anis", "Birne", "Bringebaer"
)
如果我键入“A”,可能的标题列表应该减少到“Apple”,“Ananas”和“Anis”,如果我键入“B”,它应该是“Banana”,“Birne”和“Bringebaer”。
键入“An”应该是“Ananas”和“Anis”。
这可行吗?
我的第一次尝试是使用`selectizeInput`并使用参数`options`:
(代码是使用ChatGPT的帮助生成的,因为我不熟悉`javascript`)
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
selectizeInput(
inputId = "header",
label = "Select Header",
choices = NULL,
options = list(
placeholder = "Type to filter headers...",
create = TRUE,
plugins = list("remove_button"),
filterOption = I("function(phrase, item) { return item.text.startsWith(phrase.toLowerCase()); }")
)
),
textOutput("selectedHeader")
)
server <- function(input, output, session) {
# 示例标题列表
headers <- c(
"Apple", "Banana", "Carrot", "Date", "Eggplant", "Fig", "Grape",
"Honeydew", "Iceberg lettuce", "Jackfruit", "Kiwi", "Lemon",
"Ananas", "Anis", "Birne", "Bringebaer"
)
# 根据过滤后的标题更新selectizeInput选项
observe({
updateSelectizeInput(
session = session,
inputId = "header",
choices = headers,
server = TRUE
)
})
# 渲染所选标题
output$selectedHeader <- renderText({
input$header
})
}
shinyApp(ui, server)
另一个提示是使用自定义JavaScript函数来处理过滤逻辑。但在这里,我完全不了解代码。
感谢任何帮助!
英文:
Hei,
I have a very long list of headers (>1000 items). I want to chose one of them with a selectInput
in a R shiny app. Before chosing one Item, I want to type letters in the selectInput
to shorten the list of headers. This is the usual behaviuor of selectInput
.
My aim is, that only items are listed in the preselect list which starts with the letters written in the input-field of the selectInput
. This is, that the users can work easily like in a dictionary only with headers beginning with a special letter.
For example my headers are:
headers <- c(
"Apple", "Banana", "Carrot", "Date", "Eggplant", "Fig", "Grape",
"Honeydew", "Iceberg lettuce", "Jackfruit", "Kiwi", "Lemon",
"Ananas", "Anis", "Birne", "Bringebaer"
)
If I type in "A" the list of possible headers should be reduced to "Apple", "Ananas","Anis" and if I type in "B" it should be "Banana","Birne","Bringebaer" only.
By typing in "An" it should be "Ananas","Anis".
Is this possible?
My first try is with selectizeInput
using argument options
:
(The code is generated wih help of ChatGPT, because I am not familiar with javascript
)
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
selectizeInput(
inputId = "header",
label = "Select Header",
choices = NULL,
options = list(
placeholder = "Type to filter headers...",
create = TRUE,
plugins = list("remove_button"),
filterOption = I("function(phrase, item) { return item.text.startsWith(phrase.toLowerCase()); }")
)
),
textOutput("selectedHeader")
)
server <- function(input, output, session) {
# Example list of headers
headers <- c(
"Apple", "Banana", "Carrot", "Date", "Eggplant", "Fig", "Grape",
"Honeydew", "Iceberg lettuce", "Jackfruit", "Kiwi", "Lemon",
"Ananas", "Anis", "Birne", "Bringebaer"
)
# Update the selectizeInput choices based on the filtered headers
observe({
updateSelectizeInput(
session = session,
inputId = "header",
choices = headers,
server = TRUE
)
})
# Render the selected header
output$selectedHeader <- renderText({
input$header
})
}
shinyApp(ui, server)
Another hint was, to use a custom JavaScript function to handle the filtering logic. But here I'm totally lost to understand the code.
Thank you for any help!
答案1
得分: 1
The option to define is the score
option. It must return a function that returns a score. Here, my function returns 1 for items that start with the searching pattern, otherwise 0.
英文:
The option to define is the score
option. It must returns a function which returns a score. Here my function returns 1 for the items which starts with the searching pattern, otherwise 0.
selectizeInput(
inputId = "header",
label = "Select Header",
choices = NULL,
multiple = TRUE,
options = list(
placeholder = "Type to filter headers...",
#create = TRUE,
plugins = list("remove_button"),
score = I("function(search) {
return function(item) {
return item.label.toLowerCase().startsWith(search) ? 1 : 0 ;
};
}")
)
),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论