Does that work for you? I removed the button as now the plot just shows by default with all areas and streets selected. If you change the input in the dropdown lists the graph adjusts correspondingly. You could also make the Street input update depending on your Area input as for some areas (e.g. C) you don't have all streets.
df <- structure(list(Col = 1:11, Mon = c(47L, 110L, 31L, 72L, 129L,
41L, 85L, 123L, 14L, 152L, 118L), Tue = c(164L, 168L, 146L, 140L,
185L, 77L, 26L, 15L, 23L, 116L, 101L), Wed = c(163L, 5L, 109L,
170L, 37L, 96L, 41L, 188L, 163L, 82L, 5L), Area = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L), .Label = c("A", "B",
"C", "D"), class = "factor"), Street = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Ste 1", "Ste 2"), class =
"factor")), class = "data.frame", row.names = c(NA,
-11L))
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
tabsetPanel(tabPanel("Week",titlePanel(h4("Trend of
Consumption",align="center")),
sidebarLayout(
sidebarPanel(#actionButton("trend", h6("Plot Trend"),width = 120),
#br(),
selectInput("area", h6("Area"),choices = c("All",levels(df$Area)),
width = 150, multiple = TRUE, selected = "All"),
selectInput("street", h6("Street"),choices =
c("All",levels(df$Street)),width =
150,multiple = TRUE, selected = "All"),
width = 2,height = 1000),
mainPanel(plotOutput("graph"))
)
)))
server <- function(input, output, session) {
# graph <- eventReactive(
# input$trend,{
# ggplot(data=df,aes(Col))+geom_line(aes(y=Mon, colour =
# "Mon"))+geom_line(aes(y=Tue, colour = "Tue"))+geom_line(aes(y=Wed,
# colour = "Wed"))+ylab("hj")
# }
# )
output$graph <- renderPlot(
{
req(input$area, input$street)
if (input$area == "All") {
area_filter <- TRUE
} else {
area_filter <- df$Area %in% input$area
}
if (input$street == "All") {
street_filter <- TRUE
} else {
street_filter <- df$Street %in% input$street
}
df_to_plot <- df %>%
filter(area_filter, street_filter)
ggplot(data=df_to_plot, aes(Col))+geom_line(aes(y=Mon, colour =
"Mon"))+geom_line(aes(y=Tue, colour = "Tue"))+geom_line(aes(y=Wed,
colour = "Wed"))+ylab("hj")
}
)
}
shinyApp(ui, server)