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.