Row

Row

---
title: "Gapminder data using crosstalk"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    theme: lumen
    source_code: embed
---

```{r setup, include=FALSE}
library(tidyverse) ; library(gapminder) ; library(countrycode) ; library(sf) ; library(rnaturalearth) ; library(crosstalk) ; library(leaflet) ; library(d3scatter) ; library(DT)

df <- gapminder %>% filter(year == 2007) %>% 
  mutate(iso_a3 = countrycode(country, "country.name", "iso3c"), 
         gdpPercap = round(gdpPercap, 0), 
         lifeExp = round(lifeExp, 0))

world <- ne_countries(type = "countries",  returnclass = 'sf') %>% 
  left_join(., df, by = "iso_a3", sort = FALSE) %>% 
  filter(!is.na(country)) %>% 
  select("country", "continent" = "continent.y", "year", "lifeExp", "pop", "gdpPercap", "geometry") %>% 
  as('Spatial')

world_NA <- ne_countries(type = "countries",  returnclass = 'sf')

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

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

```{r}
filter_slider("lifeExp", "Life expectancy (years)", sd_df, ~lifeExp)
filter_slider("gdpPercap", "Income per person ($)", sd_df, ~gdpPercap)
```

---

This [R Markdown](http://rmarkdown.rstudio.com/) document uses the experimental [crosstalk](https://github.com/rstudio/crosstalk) R package to enable interactivity between different widgets that display the same [Gapminder](https://www.gapminder.org/data/) data.

**Credits:**    
The following packages were used: 
[gapminder](https://cran.r-project.org/web/packages/gapminder/index.html),
[flexdashboard](https://cran.r-project.org/web/packages/flexdashboard/index.html),
[tidyverse](https://cran.r-project.org/web/packages/tidyverse/index.html),
[countrycode](https://cran.r-project.org/web/packages/countrycode/index.html),
[sf](https://cran.r-project.org/web/packages/sf/index.html), 
[rnaturalearth](https://cran.r-project.org/web/packages/rnaturalearth/index.html)
[leaflet](https://cran.r-project.org/web/packages/leaflet/index.html), [d3scatter](https://github.com/jcheng5/d3scatter), [DT](https://cran.r-project.org/web/packages/DT/index.html). 


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

###

```{r}
pal <- colorFactor(c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd"), domain = c("Africa", "Americas", "Asia", "Europe", "Oceania"), ordered = FALSE)

leaflet(sd) %>% 
  setView(9.998176, 14.531777, zoom = 2) %>%
  addProviderTiles("CartoDB.Positron") %>% 
  addPolygons(data = world_NA, color = "#969696", weight = 1, fillColor = "#808080") %>% 
  addPolygons(color = "#969696", weight = 2, fillColor = ~pal(continent), fillOpacity = 0.8)
```

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

###

```{r}
d3scatter(sd_df, x = ~gdpPercap, y = ~lifeExp, color = ~continent, x_label = "Income per person", y_label = "Life expectancy")
```

###

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