---
title: "Crosstalk + Layers"
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
social: [ "twitter", "facebook", "menu" ]
---
```{r setup, include=FALSE}
# crosstalk03
# adds layer of zipcode boundaries of zips with mean income >= $200k
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(leaflet)
library(DT)
library(plotly)
library(crosstalk)
library(rgdal)
library(rgeos)
```
```{r data}
# load example data
df <- read_csv("https://raw.githubusercontent.com/majerus/NEDRA2018/master/example.csv")
# clean example data
df <-
df %>%
filter(!is.na(officer)) %>%
filter(status != "Not a Prospect") %>%
select(name, class, constituency, connection, status, priority, cap16, officer, latitude, longitude)
# Wrap data in SharedData to use with crosstalk
sd <- SharedData$new(df)
```
Inputs {.sidebar}
-------------------------------------
```{r filters}
# Filter input - Officer
filter_select(id = "officer",
label = "Select Gift Officer(s)",
sharedData = sd,
group = ~officer)
# Filter input - Status
filter_select(id = "status",
label = "Select Status",
sharedData = sd,
group = ~status)
# Filter checkbox - constituency
filter_checkbox(id = "cons",
label = "Constituency",
sharedData = sd,
group = ~constituency,
inline = TRUE)
# Filter slider - capital giving 2016
filter_slider(id = "giving",
label = "2016 Giving",
sharedData = sd,
column = ~cap16)
```
Row
-------------------------------------
### Map
```{r map_prep, include=FALSE}
# mapping polygons
# load geojson for zipcodes with mean income of $200k+
wealthy_zips <- readOGR("https://raw.githubusercontent.com/majerus/NEDRA2018/master/wealthiest_100_zipcodes_geojson.GeoJSON", "OGRGeoJSON")
```
```{r map}
sd %>%
leaflet() %>%
addProviderTiles("CartoDB") %>%
setView(lat = 39.8282, lng = -98.5795, zoom = 4) %>%
addCircleMarkers(popup = ~name, group = "Prospects", radius = 2) %>%
addPolygons(data=wealthy_zips,
fillColor = "Green",
weight = 2,
opacity = 1,
color = "Green",
dashArray = "3",
fillOpacity = .7,
group = "Boundaries") %>%
addLayersControl(
overlayGroups = c("Prospects", "Boundaries"))
```
### Chart
```{r plot}
plot_ly(sd, x = ~status) %>% add_histogram()
```
Row
-------------------------------------
### Table
```{r table}
datatable(sd, extensions= c('Buttons', "Scroller"),
class="compact", width="100%", rownames = FALSE,
options=list(
dom = 'Blfrtip',
deferRender=TRUE,
scrollY=300,
scroller=TRUE,
columnDefs = list(list(visible=FALSE, targets=c(8, 9))),
buttons = list(I('colvis'), 'csv', 'excel'))) %>%
formatCurrency('cap16', digits = 0)
```