按R Shiny输入中的Enter键为搜索关键字选择多个项目

时间:2018-03-12 04:18:07

标签: r shiny reactive-programming

我正在尝试使用输入的搜索关键字选择Shiny选择输入中的项目,然后按Enter键选择关键字的所有匹配项目。

如果我提供列表中已存在的observe之类的项目但我希望它适用于任何类型的关键字,则代码段中的ALL功能会起作用。例如App然后按Enter键选择所有匹配的项目。

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

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}

    ```

1 个答案:

答案 0 :(得分:2)

我找到selectize.js的帮助。它被链接在Shiny的selectize页面上。

Tags from selectize.js 我最终使用create函数来使其工作。不得不使用callback代替return。基于搜索字符串的选择显示undefined,我无法显示正确的选择。但由于我有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

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