R Shiny selectInput – 预选列表仅以输入字段中的字母开头。

huangapple go评论55阅读模式
英文:

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”,“AnanasAnis”,如果我键入B”,它应该是Banana”,“BirneBringebaer”。
键入An应该是AnanasAnis”。

这可行吗

我的第一次尝试是使用`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 &lt;- c(
&quot;Apple&quot;, &quot;Banana&quot;, &quot;Carrot&quot;, &quot;Date&quot;, &quot;Eggplant&quot;, &quot;Fig&quot;, &quot;Grape&quot;,
&quot;Honeydew&quot;, &quot;Iceberg lettuce&quot;, &quot;Jackfruit&quot;, &quot;Kiwi&quot;, &quot;Lemon&quot;,
&quot;Ananas&quot;, &quot;Anis&quot;, &quot;Birne&quot;, &quot;Bringebaer&quot;
)

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 &lt;- fluidPage(
selectizeInput(
inputId = &quot;header&quot;,
label = &quot;Select Header&quot;,
choices = NULL,
options = list(
placeholder = &quot;Type to filter headers...&quot;,
create = TRUE,
plugins = list(&quot;remove_button&quot;),
filterOption = I(&quot;function(phrase, item) { return item.text.startsWith(phrase.toLowerCase()); }&quot;)
)
),
textOutput(&quot;selectedHeader&quot;)
)
server &lt;- function(input, output, session) {
# Example list of headers
headers &lt;- c(
&quot;Apple&quot;, &quot;Banana&quot;, &quot;Carrot&quot;, &quot;Date&quot;, &quot;Eggplant&quot;, &quot;Fig&quot;, &quot;Grape&quot;,
&quot;Honeydew&quot;, &quot;Iceberg lettuce&quot;, &quot;Jackfruit&quot;, &quot;Kiwi&quot;, &quot;Lemon&quot;,
&quot;Ananas&quot;, &quot;Anis&quot;, &quot;Birne&quot;, &quot;Bringebaer&quot;
)
# Update the selectizeInput choices based on the filtered headers
observe({
updateSelectizeInput(
session = session,
inputId = &quot;header&quot;,
choices = headers,
server = TRUE
)
})
# Render the selected header
output$selectedHeader &lt;- 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 = &quot;header&quot;,
    label = &quot;Select Header&quot;,
    choices = NULL,
    multiple = TRUE,
    options = list(
      placeholder = &quot;Type to filter headers...&quot;,
      #create = TRUE,
      plugins = list(&quot;remove_button&quot;),
      score = I(&quot;function(search) { 
        return function(item) {
          return item.label.toLowerCase().startsWith(search) ? 1 : 0 ;
        };
      }&quot;)
    )
  ),

huangapple
  • 本文由 发表于 2023年5月25日 16:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定