我正在开发一个闪亮的应用程序,我想将两个图链接到一个selectInput。因此,用户选择将同时更新两个图形。
有没有办法做到这一点?
我的代码
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)
}
我尝试将两个图形(c)和(p)添加到一个renderPlotly下,但似乎并不能解决问题。
谢谢
答案 0 :(得分:0)
我尝试将两个图形(c)和(p)添加到一个renderPlotly下,但似乎并没有达到目的。
我建议不要这样做。
选项1.使用两个renderPlotly
调用,两者均取决于相同的input$select
选项2:创建一个mydata <- reactive({...})
对象,其中包含您的数据处理步骤。然后在您的两个单独的renderPlotly
调用中,引用mydata()
。每当进行新选择时,都会更新此内容。