Row

Row

---
title: "Middle East Route"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    theme: lumen
    source_code: embed
---

```{r setup, include=FALSE}
# devtools::install_github("dmurdoch/leaflet@crosstalk4")
# install.packages(c("tidyverse", "crosstalk", "rnaturalearth", "rgdal", "geosphere"))
library(tidyverse) 
library(crosstalk)
library(leaflet)
library(rnaturalearth) 
library(rgdal)
library(geosphere)
library(RColorBrewer)
library(DT)

set.seed(1983)

df2 <- read_csv("me_route.csv")


countries <- ne_countries()

countries$longitude <- coordinates(countries)[,1]

countries$latitude <- coordinates(countries)[,2]

countries_xy <- countries@data %>%
    select(admin, longitude, latitude)


df3 <- df2 %>%
    left_join(countries_xy, by = c('origins' = 'admin')) %>%
    left_join(countries_xy, by = c('destinations' = 'admin'))





flows <- gcIntermediate(df3[,5:6], df3[,7:8], sp = TRUE, addStartEnd = TRUE)

flows$products <- df3$products

flows$origins <- df3$origins

flows$destinations <- df3$destinations

flows$counts <- df3$counts



sd <-  SharedData$new(flows)
sd_df <- SharedData$new(flows@data, group = sd$groupName())
```

Inputs {.sidebar}
-----------------------------------------------------------------------

```{r}
filter_checkbox("products", "Products", sd_df, ~products, allLevels = TRUE)
filter_checkbox("from", "From", sd_df, ~origins, allLevels = TRUE)
filter_checkbox("to", "To", sd_df, ~destinations, allLevels = TRUE)
```

---


Row {data-height=550}
-------------------------------------

###

```{r}

pal <- colorFactor(brewer.pal(4, 'Set2'), sd_df$data()$products)

hover <- paste0(sd_df$data()$origins, " to ", 
                sd_df$data()$destinations, ': ', 
                sd_df$data()$products, " | ",
                as.character(sd_df$data()$counts))

 leaflet(sd) %>%
    addProviderTiles('CartoDB.Positron') %>%
    addPolylines(weight = ~counts, 
                 color = ~pal(products),
                 label = hover)
```

Row {data-height=450}
-------------------------------------

###

```{r}
datatable(sd_df, rownames = FALSE, extensions = 'Scroller',
         options = list(scrollY = 200, scroller = TRUE, columnDefs = list(list(className = 'dt-left', targets = 0:3))))
```