r - 在 R Shiny 输入中按 Enter 键选择多个搜索关键字项目

标签 r shiny reactive-programming

我试图通过使用输入的搜索关键字并按 Enter 键选择关键字的所有匹配项来选择 Shiny 的选择输入中的项。

如果我提供列表中已存在的类似 ALL 的项目,但我希望它适用于任何键入的关键字,则代码片段中的 observe 函数将起作用。例如App 并按 Enter 键选择所有匹配的项目。

看看是否有其他自定义选项可以使用 jquery 或其他东西进行编码来捕获键入的输入并捕获过滤的项目,这将是很有趣的。或者可能是一些正则表达式,而不是我在 if 条件中使用的 "ALL"

enter image description here

    ---
    title: "search and select multiple items by pressing Enter"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    runtime: shiny
    ---

    ```{r setup, include=FALSE}
    library(flexdashboard)
    ```

    Column {.sidebar data-width=300}
    -----------------------------------------------------------------------

    ```{r}
    #####################
    ### Reactive Parameters 

    Parameters <- reactive({
      c("ALL","Apple","App","Application","Approximate","Appointment","Ap_titude","Apricot","B","Ball","Bat","Battery")
    })

    output$params = renderUI({
      selectInput(
        'params',
        'Parameters',
        choices = Parameters(),
        multiple = TRUE,
        selectize = TRUE
      )
    })

    observe({
      if("ALL" %in% input$params){
        param_selection <- setdiff(Parameters(), "ALL")
      } else {
        param_selection <- input$params
      }
      updateSelectInput(session, "params", selected = as.character(unlist(param_selection)))
    })


    uiOutput("params")

    ```

    Column
    -----------------------------------------------------------------------

    ### Summary

    ```{r}

    ```

最佳答案

我找到了 selectize.js 的帮助。它的超链接位于 selectize Shiny 的页面。

Tags from selectize.js 我最终使用create函数来让它工作。必须使用callback而不是return。基于搜索字符串的选择显示未定义,我无法让它显示正确的选择。但由于我有 observe 函数,通过它我可以 updateSelectInput,所以我并不担心这一点。

这是我整理的示例代码。

    ---
    title: "search and select multiple items by pressing Enter"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    runtime: shiny
    ---

    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(dplyr)
    ```

    Column {.sidebar data-width=300}
    -----------------------------------------------------------------------

    ```{r echo=FALSE}
    #####################
    ### Reactive Parameters 

    Parameters <- reactive({
      c("ALL","Apple","App","Application","Approximate","Appointment","Ap_titude","Apricot","B","Ball","Bat","Battery")
    })

    output$params = renderUI({
      selectizeInput(
        'params',
        'Parameters',
        selected = NULL,
        choices = Parameters(),
        multiple = TRUE,
        options = list(
        delimiter= ',',
        persist= FALSE,
        create = I("function(input, callback) {
            callback({
                'value': input,
                'text': input
            });
        }")
        )    
      )
    })

    observe({
      dt <- as.character(unlist(Parameters()))
      if(is.null(input$params)){
        return()
      } else{
          if("ALL" %in% input$params){
            param_selection <- setdiff(dt, "ALL")
          } else {
            param_selection <- dt[grep(paste(input$params, collapse = "|"), dt)]
          }
      }
      updateSelectInput(session, "params", selected = as.character(unlist(param_selection)))
    })


    uiOutput("params")

    ```

    Column
    -----------------------------------------------------------------------

    ### Summary

    ```{r}

    ```

这是输出:

搜索字符串-“App”,添加它

enter image description here

单击“添加应用程序”后,观察函数触发器并将选择更新为与关键字匹配的所有值。

enter image description here

希望这可以帮助其他像我一样面临同样问题的人。

关于r - 在 R Shiny 输入中按 Enter 键选择多个搜索关键字项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49228090/

相关文章:

R删除pheatmap的边界

r - R Shiny 中的多个绘图

r - 分析 Shiny 的服务器日志以创建使用情况统计信息

java - RxJava2 concat() 只运行第一个 Observable

javascript - 在 react 中显示 JSON

r - unzip() 正在覆盖文件,即使使用 overwrite = FALSE

r - 从 mer 对象中提取观察次数和随机效应模式

r - 将 RCurl 与 SFTP 一起使用

javascript - 在 R Sortable 中为可排序对象文本添加索引

spring - 如何取消正在进行的 Spring Flux?