图没有出现
Graph is not appearing
我正在尝试创建一个图表,我可以在其中按年份查看总航班数,并按出发地和目的地进行过滤。
Picture1: What I envision VS Picture2: The result.
问题一:图表无法出现在 shinyApp 上。
问题 2:当图表出现时,即使我 select 来自下拉框的不同来源,图表也没有改变
我对 shiny 很陌生,如有任何帮助,我们将不胜感激!我使用的数据来自哈佛数据库- data expo 2009, Airline on time data.
library(DBI)
library(ggplot2)
library(dplyr)
library(RSQLite)
#q3 data from sql
q3m <- dbGetQuery(conn, "
SELECT Year, Origin, Dest, COUNT(FlightNum) AS Total
FROM ontime
WHERE Cancelled = 0 AND Diverted = 0
GROUP BY Origin,Dest,Year
")
q3m
#ggplot
q3plot <- ggplot(q3m) +
geom_col(aes(x= Year, y = Total))+
scale_x_discrete(limits = c(2000,2001)) +
facet_wrap(~Dest) +
labs(title = paste(q3m$Origin , "to" ) , x = "Year", y = "Total Flights") +
geom_text(aes(x = Year, y = Total, label = Total), vjust = 1.5, size = 3.5, color = "white")
q3plot
闪亮
library(shiny)
server <- function(input,output, session) {
#data
data <- reactive({
req(input$sel_q3)
q3m
})
#plot
output$plot <- renderPlot({
q3plot <- ggplot(data(),aes(x= Year, y = Total)) + scale_x_discrete(limits = c(2000,2001)) +
facet_wrap(~Dest) +
geom_col()
})
#update dynamically
observe({
updateSelectInput(session, "sel_q3", choices = q3m$Origin)
})
}
ui <- fluidPage(
h1("Comparison of total flights of airport"),
selectInput(inputId = "sel_q3",
label = "Select your airport",
"Names"),
plotOutput("plot")
)
shinyApp(ui=ui, server = server)
SHINY 显示图表但图表没有改变
我所做的只是将 q3plot 放入 output$plot 而不是重新输入 ggplot
server <- function(input,output, session) {
#data
data <- reactive({
req(input$sel_q3)
q3m
})
#plot
output$plot <- renderPlot({
q3plot
})
#update dynamically
observe({
updateSelectInput(session, "sel_q3", choices = q3m$Origin)
})
}
ui <- fluidPage(
h1("Comparison of total flights of airport"),
selectInput(inputId = "sel_q3",
label = "Select your airport",
"Names"),
plotOutput("plot")
)
shinyApp(ui=ui, server = server)
感谢@MrFlick
如果有人遇到同样的问题,解决方案基本上是添加适当的过滤
#data
data <- reactive({
req(input$sel_q3)
df <- q3m %>% filter(Origin %in% inputsel_q3) # <- this is the fix
})
#plot
output$plot <- renderPlot({
ggplot(data(),aes(x,y)) + geomcol()
})
我正在尝试创建一个图表,我可以在其中按年份查看总航班数,并按出发地和目的地进行过滤。 Picture1: What I envision VS Picture2: The result.
问题一:图表无法出现在 shinyApp 上。
问题 2:当图表出现时,即使我 select 来自下拉框的不同来源,图表也没有改变
我对 shiny 很陌生,如有任何帮助,我们将不胜感激!我使用的数据来自哈佛数据库- data expo 2009, Airline on time data.
library(DBI)
library(ggplot2)
library(dplyr)
library(RSQLite)
#q3 data from sql
q3m <- dbGetQuery(conn, "
SELECT Year, Origin, Dest, COUNT(FlightNum) AS Total
FROM ontime
WHERE Cancelled = 0 AND Diverted = 0
GROUP BY Origin,Dest,Year
")
q3m
#ggplot
q3plot <- ggplot(q3m) +
geom_col(aes(x= Year, y = Total))+
scale_x_discrete(limits = c(2000,2001)) +
facet_wrap(~Dest) +
labs(title = paste(q3m$Origin , "to" ) , x = "Year", y = "Total Flights") +
geom_text(aes(x = Year, y = Total, label = Total), vjust = 1.5, size = 3.5, color = "white")
q3plot
闪亮
library(shiny)
server <- function(input,output, session) {
#data
data <- reactive({
req(input$sel_q3)
q3m
})
#plot
output$plot <- renderPlot({
q3plot <- ggplot(data(),aes(x= Year, y = Total)) + scale_x_discrete(limits = c(2000,2001)) +
facet_wrap(~Dest) +
geom_col()
})
#update dynamically
observe({
updateSelectInput(session, "sel_q3", choices = q3m$Origin)
})
}
ui <- fluidPage(
h1("Comparison of total flights of airport"),
selectInput(inputId = "sel_q3",
label = "Select your airport",
"Names"),
plotOutput("plot")
)
shinyApp(ui=ui, server = server)
SHINY 显示图表但图表没有改变
我所做的只是将 q3plot 放入 output$plot 而不是重新输入 ggplot
server <- function(input,output, session) {
#data
data <- reactive({
req(input$sel_q3)
q3m
})
#plot
output$plot <- renderPlot({
q3plot
})
#update dynamically
observe({
updateSelectInput(session, "sel_q3", choices = q3m$Origin)
})
}
ui <- fluidPage(
h1("Comparison of total flights of airport"),
selectInput(inputId = "sel_q3",
label = "Select your airport",
"Names"),
plotOutput("plot")
)
shinyApp(ui=ui, server = server)
感谢@MrFlick 如果有人遇到同样的问题,解决方案基本上是添加适当的过滤
#data
data <- reactive({
req(input$sel_q3)
df <- q3m %>% filter(Origin %in% inputsel_q3) # <- this is the fix
})
#plot
output$plot <- renderPlot({
ggplot(data(),aes(x,y)) + geomcol()
})