Home > Enterprise >  Graph is not appearing
Graph is not appearing

Time:02-22

I am trying to create a graph where I am can view total flights by year, filtered by origin and its destination. Picture1: What I envision VS Picture2: The result.

Problem 1: Graph unable to appear on shinyApp.

Problem 2: When the graph appears, the graph does not change even when I select a different Origin from the dropdown box

I am quite new to shiny, any help would be greatly appreciated! The data I am using is from Harvard database- 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

SHINY

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 shows graph but the graph does not change

What i did was just put q3plot into output$plot instead of retyping 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)

CodePudding user response:

Thanks to @MrFlick The solution if anyone faces the same issue is basically adding the proper filtering

#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()
  })
  • Related