I have a solution, but it might not be exactly what you're looking for. In order to allow for the "All" option for each filtered variable, I'm using selectInput rather than selectizeInput. I'm not sure how to allow for the option of multiple selections while also having an "All" option in the filtering criteria. Someone else might have a good idea for that. As with most Shiny apps, there are multiple ways of doing this. This is a bit clunky (lots of steps), but the other option (as I see it) is multiple if/else steps in one reactive function. And that has the habit of going wrong occasionally.
I include the choices embedded within the selectInput functions in the UI rather than at the top but you can put it back up there instead. When you're adding an "All" option to a filter you need to convert from a factor to a character, which is what's happening there. I think you also forgot to use the right variable name for the year in the original filter, so I changed that so it would work for me.
library(shiny)
library(shinythemes)
library(dplyr)
library(DT)
library(here)
load(here::here('apps', 'movies', 'movies.RData'))
min_year <- min(movies$thtr_rel_year)
max_year <- max(movies$thtr_rel_year)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(width = 2,
selectInput(
inputId = "i_title_type",
label = "Title type:",
choices = c("All", unique(as.character(movies$title_type))),
selected = "All"
),
selectInput(
inputId = "i_genre",
label = "Genre:",
choices = c("All", unique(as.character(movies$genre))),
selected = "All"
),
selectInput(
inputId = "i_studio",
label = "Studio:",
choices = c("All", unique(as.character(movies$studio))),
selected = "All"
),
selectInput(
inputId = "i_mpaa_rating",
label = "MPAA rating:",
choices = c("All", unique(as.character(movies$mpaa_rating))),
selected = "All"
),
sliderInput(
inputId = "i_year",
label = "Year", min = min_year, max = max_year,
sep = "",
value = c(1995, 2000)
),
br(),
actionButton('select', 'Select'),
hr(),
downloadButton("download", "Download results")
),
mainPanel(width = 10,
DT::dataTableOutput(outputId = "mtable")
)
)
)
server <- function(input, output) {
filtered_title_type <- reactive({
if(input$i_title_type == "All"){
movies
} else {
movies %>%
filter(title_type == input$i_title_type)
}
})
filtered_genre <- reactive({
if(input$i_genre == "All"){
filtered_title_type()
} else {
filtered_title_type() %>%
filter(genre == input$i_genre)
}
})
filtered_studio <- reactive({
if(input$i_studio == "All"){
filtered_genre()
} else {
filtered_genre() %>%
filter(studio == input$i_studio)
}
})
filtered_rating <- reactive({
if(input$i_mpaa_rating == "All"){
filtered_studio()
} else {
filtered_studio() %>%
filter(mpaa_rating == input$i_mpaa_rating)
}
})
filtered_year <- reactive({
filtered_rating() %>%
filter(thtr_rel_year >= input$i_year[1] & thtr_rel_year <= input$i_year[2]) %>%
select(title:thtr_rel_year)
})
fully_filtered <- eventReactive(input$select, {
filtered_year()
})
output$mtable <- DT::renderDataTable({
DT::datatable(data = fully_filtered(), options = list(pageLength = 10),
rownames = FALSE, class = 'display', escape = FALSE)
})
output$download <- downloadHandler(
filename = function() {
"movie-results.csv"
},
content = function(con) {
write.csv(fully_filtered(), con)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
Created on 2019-07-26 by the reprex package (v0.2.1)