I have the following shiny app - my goal is to have an option in my inputs that does not filter named "all". The problem is that I can't figure out how to code the "All" function. I can filter down to a specific country no problem, but when I select "All" I get a blank plot.
library(shinydashboard)
library(shiny)
library(ggplot2)
library(dplyr)
data <- data.frame(Country = c("France", "Germany","England","Spain"),
Count = c(10,12,15,9))
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenuOutput("menu")
),
dashboardBody(
fluidRow(
box(plotOutput("plot1", height = 250)),
box(selectInput("Dropdown","Select Country", c(data$Country, "All"))
)
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(data %>%
filter(Country == input$Dropdown),
aes(x=Country, y=Count)) +
geom_bar(stat = "identity")
})
}
shinyApp(ui, server)
Here is some code I have tried which does not work
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(data %>%
filter{if (input$Dropdown == "All") data
else (Country == input$Dropdown)} ,
aes(x=Country, y=Count)) +
geom_bar(stat = "identity")
})
}
You should not filter when it is "All". Try this
output$plot1 <- renderPlot({
if (c("All") %in% input$Dropdown) df <- data
else df <- data %>% filter(Country == input$Dropdown)
ggplot(df, aes(x=Country, y=Count)) +
geom_bar(stat = "identity")
})