Before we jump in, I put this reprex together with mpg which might have not been the best choice of data for this exercise, but bear with me.
I'm trying to create a Shiny app that adapts to whether or not something is checked out of each of the checkboxGroupInput groups. However, when I run the app I get no errors and no plot either.
The goal is for the app to look at the checkbox selections, run a dplyr filter and tally, and then plot that new dataframe in a bar chart. If nothing is selected in one of the checkbox groups, offer up a bar chart of the other. If something is checked from both groups, one becomes the X, the other becomes the fill.
Can anybody pinpoint where I'm going wrong in this reprex?
library(shiny)
library(tidyverse)
ui <- fluidPage("Car breakdown",
sidebarLayout(
sidebarPanel(
checkboxGroupInput("manuSelector", "Manufacturers to show:",
choices = c("Audi" = "audi",
"Chevrolet" = "chevrolet",
"Dodge" = "dodge",
"Ford" = "ford",
"Honda" = "honda",
"Hyundai" = "hyundai",
"Jeep" = "jeep",
"Land Rover" = "land rover",
"Lincoln" = "lincoln",
"Mercury" = "mercury",
"Nissan" = "nissan",
"Pontiac" = "pontiac",
"Subaru" = "subaru",
"Toyota" = "toyota",
"Volkswagen" = "volkswagen"),
selected = c("ford", "chevrolet")
),
checkboxGroupInput("classSelector", "Classes to show:",
choices = c("Two-seater" = "2seater",
"Compact" = "compact",
"Mid-size" = "midsize",
"Minivan" = "minivan",
"Pickup" = "pickup",
"Sub-compact" = "subcompact",
"SUV" = "suv"),
selected = c("2seater", "midsize", "pickup")
)
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
filteredData <- reactive({
if (length(input$manuSelector) == 0){
mpg %>%
filter(class %in% input$classSelector) %>%
tally()
}
else if (length(input$classSelector) == 0){
mpg %>%
filter(manufacturer %in% input$manuSelector) %>%
tally()
}
else {
mpg %>%
filter(class %in% input$classSelector, manufacturer %in% input$manuSelector) %>%
tally()
}
})
output$distPlot <- renderPlot({
ifelse(length(input$manuSelector == 0),
ggplot(data = filteredData(), aes(x = class, y = n)) +
geom_bar(stat = "identity"),
ifelse(length(input$classSelector == 0),
ggplot(data = filteredData(), aes(x = manufacturer, y = n)) +
geom_bar(stat = "identity"),
ggplot(data = filteredData(), aes(x = manufacturer, y = n, fill = class)) +
geom_bar(stat = "identity")
)
)
})
}
shinyApp(ui = ui, server = server)
Thank you in advance!