French Train stats

Row

Trip total (Hrs.)

Total stops

Row

Shortest path

---
title: "French Train stats"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    theme: bootstrap
    source_code: embed
    social: [ "twitter" ]
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidygraph)
library(ggraph)
library(dplyr)
library(readr)
library(stringr)
library(shiny)

small_trains <- read_rds("small.rds")

st <- small_trains %>%
  group_by(departure_station, arrival_station) %>%
  summarise(journey_time = mean(journey_time_avg)) %>%
  ungroup() %>%
  mutate(from = departure_station, 
         to = arrival_station) %>%
  select(from, to, journey_time)


tg_small_trains <- as_tbl_graph(st) %>%
  activate(nodes) %>%
  mutate(
    title = str_to_title(name),
    label = str_replace_all(title, " ", "\n")
    )

stations <- tg_small_trains %>%
  activate(nodes) %>%
  pull(title)

sts <- as.list(seq_along(stations))
names(sts) <- stations

```

Column {.sidebar}
-----------------------------------------------------------------------

```{r}
selectInput(
  "station1", label = "Station 1:",
  choices = sts,
  selected = "34",
  selectize = FALSE)
```

```{r}
selectInput(
  "station2", label = "Station 2:",
  choices = sts,
  selected = "28",
  selectize = FALSE)
```

Row  {data-height=100}
-----------------------------------------------------------------------

### Trip total (Hrs.)

```{r}
valueBoxOutput("trip")
```

### Total stops

```{r}
valueBoxOutput("stops")
```

Row
-----------------------------------------------------------------------

### Shortest path

```{r}
plotOutput("map")
```


```{r server}
thm <- theme_minimal() +
  theme(
    legend.position = "none",
     axis.title = element_blank(),
     axis.text = element_blank(),
     panel.grid = element_blank(),
     panel.grid.major = element_blank(),
  ) 
theme_set(thm)

shortest <- reactive({
  station1 <- as.integer(input$station1)
  station2 <- as.integer(input$station2)
  
  tg_small_trains %>%
      morph(to_shortest_path, station1, station2, weights = journey_time) %>%
      mutate(selected_path = 1) %>%
      activate(edges) %>%
      mutate(selected_path = 1) %>%
      unmorph()  %>%
      activate(nodes) %>%
      mutate(selected_path = ifelse(is.na(selected_path), 1, 2)) %>%
      activate(edges) %>%
      mutate(selected_path = ifelse(is.na(selected_path), 1, 2)) %>%
      arrange(selected_path)
  })

output$trip <- renderValueBox({
  total_time <- shortest() %>%
  activate(edges) %>%
  filter(selected_path == 2) %>%
  pull(journey_time) %>%
  sum()
  valueBox(value = round(total_time / 60), icon = "fa-train")

})

output$stops <- renderValueBox({
  stops <- shortest() %>%
  activate(edges) %>%
  filter(selected_path == 2) %>%
  pull(journey_time) 
  valueBox(value = length(stops) - 1, icon = "fa-hand-paper", color = "#009E73")

})

output$map <- renderPlot({
  shortest() %>%
   ggraph(layout = "kk") +
   geom_edge_diagonal(aes(alpha = selected_path), color = "#999999") +
   geom_node_text(aes(label = label, color =name, alpha = selected_path, size = 12 ))
 })
```