R: Create search field Semantic UI component

search_field {shiny.semantic} | R Documentation

## Create search field Semantic UI component

### Description

This creates a default search field using Semantic UI styles with Shiny input. Search field is already initialized and available under input[[name]]. Search will automatically route to the named API endpoint provided as parameter. API response is expected to be a JSON with property fields 'title' and 'description'. See https://semantic-ui.com/modules/search.html#behaviors for more details.

### Usage

    search_field(name, search_api_url, default_text = "Search", value = "")

### Arguments

`name` | Input name. Reactive value is available under input[[name]].
`search_api_url` | Register custom API url with server JSON Response containing fields 'title' and 'description'.
`default_text` | Text to be visible on serach field when nothing is selected.
`value` | Pass value if you want to initialize selection for search field.

### Examples

    ## Only run examples in interactive R sessions
    ## Not run:
    if (interactive()) {
    library(shiny)
    library(shiny.semantic)
    library(gapminder)
    library(dplyr)

    ui <- function() {
      shinyUI(
        semanticPage(
          title = "Dropdown example",
          uiOutput("search_letters"),
          p("Selected letter:"),
          textOutput("selected_letters")
       )
      )
    }

    server <- shinyServer(function(input, output, session) {

     search_api <- function(gapminder, q){
       has_matching <- function(field) {
         startsWith(field, q)
       }
       gapminder %>%
         mutate(country = as.character(country)) %>%
         select(country) %>%
         unique %>%
         filter(has_matching(country)) %>%
         head(5) %>%
         transmute(title = country,
                   description = country)
     }

     search_api_url <- register_search(session, gapminder, search_api)
     output$search_letters <- shiny::renderUI(
       search_field("search_result", search_api_url)
     )
     output$selected_letters <- renderText(input[["search_result"]])
    })
    }

    shinyApp(ui = ui(), server = server)

    ## End(Not run)

[Package shiny.semantic version 0.2.1 Index]