我正在嘗試創建一個圖表,我可以在其中按年份查看總航班,按出發地和目的地過濾。 圖 1:我的設想VS圖2:結果。
問題1:圖形無法出現在shinyApp上。
問題2:出現圖表時,即使我從下拉框中選擇不同的Origin,圖表也不會改變
我對閃亮還是很陌生,任何幫助將不勝感激!我使用的資料來自哈佛資料庫——2009 年資料博覽會,航空公司準時資料。
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)
uj5u.com熱心網友回復:
感謝@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()
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429484.html
上一篇:是否可以將多個單變數部分依賴圖的結果組合在一個圖上?
下一篇:如何使用列中的值繪制雷達圖?
