I've been trying to create a simple shiny app. The data I'm using is a dataframe of the daily amount of flights and passengers of an airport in .xlsx, with a date column in yyyy-mm-dd format as well as a column for each the number of the day, the month and the year. I want to be able to select a date range and to receive a corresponding plot, with x=date (the input date range) and y=flights.
Here is the head():
# A tibble: 6 × 7
date passengers flights pas_per_flight month day year
<dttm> <dbl> <dbl> <dbl> <chr> <chr> <chr>
1 2019-01-01 00:00:00 75505 563 134. 1 1 2019
2 2019-01-02 00:00:00 92393 659 140. 1 2 2019
3 2019-01-03 00:00:00 81189 627 129. 1 3 2019
4 2019-01-04 00:00:00 83156 658 126. 1 4 2019
5 2019-01-05 00:00:00 78043 560 139. 1 5 2019
6 2019-01-06 00:00:00 87890 655 134. 1 6 2019
Here is the dput(head())
structure(list(date = structure(c(1546300800, 1546387200, 1546473600,
1546560000, 1546646400, 1546732800), tzone = "UTC", class = c("POSIXct",
"POSIXt")), passengers = c(75505, 92393, 81189, 83156, 78043,
87890), flights = c(563, 659, 627, 658, 560, 655), pas_per_flight = c(134.11190053286,
140.201820940819, 129.488038277512, 126.376899696049, 139.3625,
134.18320610687), month = c("1", "1", "1", "1", "1", "1"), day = c("1",
"2", "3", "4", "5", "6"), year = c("2019", "2019", "2019", "2019",
"2019", "2019")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
This is what I came up with, but I unfortunately have no error message to report. The app runs, the daterange selector is there and working, but there is no plot being put out. The "center" of the app just stays blank.
library(shiny)
library(dplyr)
library(ggplot2)
library(readxl)
zhairport<-read_xlsx("airport.xlsx")
ui <- fluidPage(
sidebarLayout(
mainPanel(plotOutput("date")),
sidebarPanel(
dateRangeInput("date", "Choose timeframe", start="2019-01-01", end="2020-12-31",
min="2019-01-01",max="2020-12-31"),
)),
)
server<-function(input, output, session){
subdata<-reactive({
zhairport %>%
filter(
as.Date(date) >= as.Date(input$date[1]),
as.Date(date) <= as.Date(input$date[2]),
)
})
output$plot<-renderPlot({
ggplot(subdata(),
aes(x=date, y=flights)
geom_line(aes(x=date,y=flights)))
})
}
shinyApp(ui = ui, server = server)
Apologies for the horrible formatting and the probably very bad code, it's my first app.
TIA for any answers!
CodePudding user response:
here is the corrected code
zhairport <-structure(list(date = structure(c(1546300800, 1546387200, 1546473600,
1546560000, 1546646400, 1546732800), tzone = "UTC", class = c("POSIXct", "POSIXt")), passengers = c(75505, 92393, 81189, 83156, 78043,87890), flights = c(563, 659, 627, 658, 560, 655), pas_per_flight = c(134.11190053286,140.201820940819, 129.488038277512, 126.376899696049, 139.3625,134.18320610687), month = c("1", "1", "1", "1", "1", "1"), day = c("1","2", "3", "4", "5", "6"), year = c("2019", "2019", "2019", "2019","2019", "2019")), row.names = c(NA, -6L), class = c("tbl_df","tbl", "data.frame"))
library(shiny)
library(dplyr)
library(ggplot2)
library(readxl)
ui <- fluidPage(
sidebarLayout(
mainPanel(plotOutput("plot")),
sidebarPanel(
dateRangeInput("date", "Choose timeframe", start="2019-01-01", end="2020-12-31",
min="2019-01-01",max="2020-12-31"),
)),
)
server<-function(input, output, session){
subdata<-reactive({
zhairport %>%
filter(
as.Date(date) >= as.Date(input$date[1]),
as.Date(date) <= as.Date(input$date[2]),
)
})
output$plot<-renderPlot({
data = subdata()
ggplot(data,
aes(x=date, y=flights)) geom_line(aes(x=date,y=flights))
})
}
shinyApp(ui = ui, server = server)