search_field: Create search field Semantic UI component

Description Usage Arguments Examples

View source: R/search_field.R

Description

This creates a default search field using Semantic UI styles with Shiny input. Search field is already initialized and available under input[[input_id]]. 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

1
search_field(input_id, search_api_url, default_text = "Search", value = "")

Arguments

input_id

Input name. Reactive value is available under input[[input_id]].

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
## 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",
      p("Search country:"),
      uiOutput("search_country"),
      p("Selected country:"),
      textOutput("selected_country")
   )
  )
}

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_country <- renderText(input[["search_result"]])
})
}

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

## End(Not run)

shiny.semantic documentation built on Nov. 7, 2021, 5:07 p.m.