Hello
I'm stuck on one part of an app I'm developing (a simplified version of it is below).
Bascially I have a textInput where the user can write one or several words that will be used to filter a character column of a dataframe using %like%.
My attempt is below but it only returns the matches from the first value if several words are listed in the textInput. For example, in my code below using mtcars, if I input Fiat, Merc it only filters on Fiat. Similarly if I input Merc, Datsun, Mazda it again only filters on Merc.
How do I get it to search using all the values in the textInput?
library(shiny)
library(stringr)
data(mtcars)
ui <- fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
textInput(inputId = "textBox",
label = "Car Search",
placeholder = "Enter cars here seperated by a comma"),
actionButton("textSearchButton", "Generate the Table")
),
tableOutput("dtOut")
)
)
server <- function(input, output) {
tblFunc <- function(x, car){
x$make <- rownames(x)
car <- str_replace(car, ", ", ",")
car <- unlist(strsplit(car, ","))
outTbl <- x[x$make %like% car, c(12, 1:4)]
return(outTbl)
}
outTable <- eventReactive(input$textSearchButton, {
req(input$textBox)
tblFunc(mtcars, input$textBox)
})
output$dtOut <- renderTable({
outTable()
})
}
shinyApp(ui, server)