library(flexdashboard)
## Warning: package 'flexdashboard' was built under R version 4.5.3
library(shiny)
## Warning: package 'shiny' was built under R version 4.5.3
library(plotly)
## Warning: package 'plotly' was built under R version 4.5.3
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## Warning: package 'lubridate' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.5     ✔ tibble    3.3.0
## ✔ purrr     1.1.0     ✔ tidyr     1.3.1
## ✔ readr     2.1.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
df_gautrain <- read.csv("gautrain_synthetic_dataset.csv")
df_gautrain_clean <- df_gautrain %>%
  mutate(
   
    start_datetime = ymd_hms(start_datetime),
    end_datetime = ymd_hms(end_datetime),
    
    fare_zar = as.numeric(fare_zar),
    
    station_entry = as.factor(station_entry),
    station_exit = as.factor(station_exit)
  ) %>%
 
  filter(!is.na(fare_zar), !is.na(station_entry), !is.na(station_exit))


station_choices <- sort(unique(as.character(df_gautrain_clean$station_entry)))
selectInput("station_select",
            "Select Station Entry:",
            choices = station_choices,
            selected = station_choices[1],
            multiple = FALSE,
            selectize = TRUE)
renderPlotly({
  
  dat <- df_gautrain_clean %>%
    filter(station_entry == input$station_select)
  
 
  validate(
    need(nrow(dat) > 0, "No data available for this station.")
  )
  
  
  plot_data <- dat %>%
    group_by(station_exit) %>%
    summarise(avg_fare = mean(fare_zar, na.rm = TRUE)) %>%
    arrange(desc(avg_fare))
  
  
  plot_ly(data = plot_data, 
          x = ~station_exit, 
          y = ~avg_fare, 
          type = 'bar', 
          marker = list(color = 'pink')) %>%
    layout(title = paste("Average Fare from", input$station_select),
           xaxis = list(title = "Exit Station"),
           yaxis = list(title = "Average Fare (ZAR)"))
})
renderText({
  
  dat <- df_gautrain_clean %>%
    filter(station_entry == input$station_select)
  
 
  total_trips <- nrow(dat)
  
  
  paste0("Total trips originating from ", 
         input$station_select, ": ", 
         format(total_trips, big.mark = ","))
})
renderTable({
  
  dat <- df_gautrain_clean %>%
    filter(station_entry == input$station_select)
  
  
  data.frame(
    Metric = c("Total Trips", "Avg Fare (All Destinations)", "Max Fare"),
    Value = c(nrow(dat), 
              round(mean(dat$fare_zar, na.rm = TRUE), 2), 
              round(max(dat$fare_zar, na.rm = TRUE), 2))
  )
}, striped = TRUE, hover = TRUE, bordered = TRUE)