2

I'm creating a visualization in Shiny based on the 'movies' dataset. In the dataset, among others, for each movie there is an attribute plot_keywords (format is murder|crime|police|man|detective - 5 words separated with | without spaces). I want to implement an interactive filter for this attribute regardless of uppercase / capital letters - that is, when you enter 'Murder', for example, Shiny should display all movies where 'murder' is present at any part of the plot_keywords attribute. In my code, if the user does not enter anything in the filter box (by default), all movies are displayed. What function should I use after 'else'?

Part of UI code

ui <- fluidPage(
fluidRow(
column(3,
    wellPanel(
      textInput("plot", "I want to watch movie about...",NULL)
      )),

Part of Server code

server <- function(input, output) {
p <- input$plot
m <- movies %>%
filter(
  if(p != NULL) && (p != "")
    {plot_keywords == movies$plot_keywords}
  else
)`
John Town
  • 23
  • 3
  • There are a few answers to this [here](https://stackoverflow.com/questions/5671719/case-insensitive-search-of-a-list-in-r) – Vlad May 23 '18 at 03:02

1 Answers1

2

One idea would be as shown below. This also allows the user to type in multiple words to search for. i.e. when the user types CrocODile Ii, the movie Crocodile Dundee II will also show up. The grepl function that we use has a parameter ignore.case to make sure that our query is case insensitive.

Hope this helps!


enter image description here


library(shiny)
library(ggplot2movies)
library(dplyr)

ui <- fluidPage(
  textInput("plot", "I want to watch movie about...",NULL),
  dataTableOutput('my_data')
)

server <- function(input, output) {
  output$my_data <- renderDataTable({
    p <- input$plot
    if(p != '')
      movies %>% filter(Reduce(`&`, lapply(strsplit(p,' ')[[1]], grepl, title,ignore.case=T)))
    else
      movies
  })
}

shinyApp(ui,server)
Florian
  • 24,425
  • 4
  • 49
  • 80