1

I want to be able to add a search widget to my app, link:https://aquayaapps.shinyapps.io/big_data/ such that when I type any word i.e. water it outputs all words containing water.

So far I have only been able to add the search widget but I don't know how to include interactivity such when I type on the search bar, results are displayed.

sidebarSearchForm(textId = "searchText", buttonId = "searchButton",label = "Search dataset",
                        icon = shiny::icon("search"))

This is the UI but I don't know how to make it interactive.

The search bar should be interactive such that when I type a word it outputs the search results.

brian
  • 75
  • 1
  • 8

1 Answers1

4

It sort of depends what output you want, but the principle would be the same. I'm assuming you have some kind of data table, and you would want any row that contains the search term.

Therefore you need:

  • An output that is a table or datatable, filtered by the input (in this case input$searchText)
  • req() if you want it to only show if you have pressed the search button

Here's a pretty ugly mock up, but hopefully you get the idea.

library(shiny)
library(shinydashboard)
library(data.table)

header <- dashboardHeader(title = "Search function")

sidebar <- dashboardSidebar(
  sidebarSearchForm(textId = "searchText", buttonId = "searchButton", 
                    label = "Search dataset", icon = shiny::icon("search"))
)

body <- dashboardBody(tableOutput("filtered_table"))

ui <- dashboardPage(title = 'Search', header, sidebar, body)


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

      example_data <- data.table(ID = 1:7, word = c("random", "words", "to", 
                                                    "test", "the", "search", "function")) 

      output$filtered_table <- renderTable({
        req(input$searchButton == TRUE)
        example_data[word %like% input$searchText]
      })

    }

    shinyApp(ui = ui, server = server)

EDIT: Just to add, if you do want a datatable visible that users can search, if you use dataTableOuput and renderDataTable from the package DT, that includes a search function with the table.

Jaccar
  • 1,720
  • 17
  • 46
  • I want to add a datatable with metadata that have links to websites which when clicked open in a new tab.The big challenge is how do I include a hyperlink to website in a word that is in a datatable? – brian May 02 '19 at 11:06
  • That is a separate question. Did this answer solve your original question? Here is an existing answer that relates to your new question: https://stackoverflow.com/questions/28117556/clickable-links-in-shiny-datatable – Jaccar May 02 '19 at 13:38