I am working on a shiny app and I have two plots that I would like to link to one selectInput. So the user selection will update both graphs.
Is there a way to achieve this?
my code
library(shiny)
library(plotly)
library(dplyr)
library(tidyr)
#ui
shinyUI(
pageWithSidebar(
headerPanel("NYC Flights Delays 2013"),
sidebarPanel(
selectInput("select", "Select a month",
choices = list("2013","January" ,"February",
"March","April",
"May", "June",
"July","August",
"Septemeber","October",
"November","December")
)),
mainPanel(
plotlyOutput("monthlyavg",width="900",height = "400px")
)
))
#server
function(input, output){
output$monthlyavg<-renderPlotly({
if (input$select =="January") {
#calculate avg delay for departures and arrivals each month
average_delay_month<-flight%>%
group_by(flight_month)%>%
summarise(Average_Departure_Delay=mean(dep_delay[dep_delay>0]),Average_Arrival_Delay=mean(arr_delay[arr_delay>0]))
#filtering according to month
average_delay_month<-subset(average_delay_month,flight_month=="January")
#average delay by month(January)
c<-average_delay_month %>%gather("metrics", "value", -flight_month)%>%
ggplot(aes(flight_month, value, group= metrics, fill = metrics)) +
geom_bar(stat = "identity", width=.5, position = "dodge")+
labs(title="Average Delay by Month", x="Flight Month",y="Average Delay in Minutes")
print(c)
#--January--#
#--All Months Flight count--#
flight$flight_month<-factor(flight$flight_month, levels=c("January", "February", "March", "April", "May",
"June","July","August",'September',"October","November","December"))
#count number of flights per month
flight_month_count<-setNames(aggregate(year~flight_month,data=flight,FUN=length),
c('Month','Flight_Count'))
flight_month_count<-subset(flight_month_count,Month=="January")
#flight month_count_graph
p<-ggplot(data=flight_month_count)+
geom_bar(stat="identity",fill="#008080",aes(Month,Flight_Count),width=0.5)+
labs(title="Total Number of Flights per Month",x="Flight Month",y="Number of Flights")
print(p)
}
I tried adding the two graphs (c) and (p) under one renderPlotly but it doesn't seem to do the trick.
Thanks