-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path04_leaflet-flexdash-dt-crosstalk.Rmd
249 lines (210 loc) · 7.64 KB
/
04_leaflet-flexdash-dt-crosstalk.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
---
title: "Leaflet + Flexdashboard + DT + Crosstalk"
author: "Matt Dray"
output:
flexdashboard::flex_dashboard:
theme: paper
favicon: img/ios7-location-outline.png
source_code: embed
---
```{r setup, include=FALSE}
# prep workspace
library(dplyr) # tidy data manipulation
library(leaflet) # interative mapping
library(DT) # interactive tables
library(crosstalk) # inter-widget interactivity
sch <- readRDS("data/gias_sample.RDS")
sd <- SharedData$new(sch)
```
Interactives {data-icon="ion-stats-bars"}
=====================================
Column {data-width=400}
-------------------------------------
### Filters
```{r filters}
filter_select(
id = "geo_la",
label = "Local authority",
sharedData = sd,
group = ~geo_la
)
bscols(
filter_checkbox(
id = "ofsted_rating",
label = "Ofsted grade",
sharedData = sd,
group = ~ofsted_rating
),
filter_checkbox(
id = "sch_phase",
label = "Phase",
sharedData = sd,
group = ~sch_phase
)
)
bscols(
filter_slider(
id = "pupil_count",
label = "Pupil count",
sharedData = sd,
column = ~pupil_count,
step = 10,
round = TRUE,
sep = "",
ticks = FALSE
),
filter_slider(
id = "pupil_percent_fsm",
label = "Percentage Free School Meals",
sharedData = sd,
column = ~pupil_percent_fsm,
step = 1,
round = TRUE,
sep = "",
ticks = FALSE
)
)
```
### Datatable
```{r datatable}
sd %>%
DT::datatable(
filter = "top", # allows filtering on each column
extensions = c(
"Buttons", # add download buttons, etc
"Scroller" # for scrolling down the rows rather than pagination
),
rownames = FALSE, # remove rownames
style = "bootstrap",
class = "compact",
width = "100%",
options = list(
dom = "Blrtip", # specify content (search box, etc)
deferRender = TRUE,
scrollY = 300,
scroller = TRUE,
columnDefs = list(
list(
visible = FALSE,
targets = c(2, 3, 5:15)
)
),
buttons = list(
I("colvis"), # turn columns on and off
"csv", # download as .csv
"excel" # download as .xlsx
)
),
colnames = c(
"URN" = "sch_urn",
"Name" = "sch_name",
"Type" = "sch_type",
"Type group" = "sch_type_group",
"Phase" = "sch_phase",
"Ofsted rating" = "ofsted_rating",
"Inspection date" = "ofsted_date",
"Pupil count" = "pupil_count",
"Pupil gender" = "pupil_gender",
"FSM per cent" = "pupil_percent_fsm",
"Town" = "geo_town",
"Postcode" = "geo_postcode",
"Local authority" = "geo_la",
"Rural-urban class" = "geo_urban_rural",
"RSC region" = "geo_rsc_region",
"Coordinates" = "geometry"
)
)
```
Column {data-width=600}
-------------------------------------
### Interactive map
```{r map}
sd %>%
leaflet::leaflet() %>%
leaflet::addProviderTiles(providers$OpenStreetMap) %>%
leaflet::addAwesomeMarkers(
popup = ~paste0(
"<h5>", sch$sch_name, "</h5>",
"<table style='width:100%'>",
"<tr>",
"<th>URN</th>",
"<th>", sch$sch_urn, "</th>",
"</tr>",
"<tr>",
"<tr>",
"<th>Phase</th>",
"<th>", sch$sch_phase, "</th>",
"</tr>",
"<tr>",
"<tr>",
"<th>Type</th>",
"<th>", sch$sch_type, "</th>",
"</tr>",
"<tr>",
"<tr>",
"<th>Location</th>",
"<th>", sch$geo_town, ", ", sch$geo_postcode, "</th>",
"</tr>",
"<tr>",
"<tr>",
"<th>LA</th>",
"<th>", sch$geo_la, "</th>",
"</tr>"
), # end popup()
icon = awesomeIcons(
library = "ion",
icon = ifelse(
test = sch$ofsted_rating == "1 Outstanding",
yes = "ion-android-star-outline",
no = "ion-android-radio-button-off"
),
iconColor = "white",
markerColor = ifelse(
test = sch$sch_phase == "Primary",
yes = "red",
no = "blue"
)
)
) %>% # end addAwesomeMarkers()
leaflet::addMeasure()
```
Information {data-orientation=rows data-icon="fa-info-circle"}
=====================================
### Blurb
This example was shown as part of the talk <i>Crosstalk: Shiny-like without Shiny</i> at the [Enterprise Applications of the R Language (EARL) conference in London, September 2018](https://earlconf.com/2018/london/#matt-dray).
> Self-service interactive tools have great power to support decisions by policy-makers. Shiny apps are a natural fit for this, but it's not always easy to share them within the public sector. This is due to issues like a lack of server space, highly sensitive data and users who aren't R-savvy.
>
>We've approached this problem in the UK's Department for Education by sharing interactive HTML widgets – embeddable JavaScript visualisation libraries – within RMarkdown outputs. Interactivity is, however, limited because selections in one widget don’t impact the data presented in another.
>
>[Joe Cheng's Crosstalk package](http://rstudio.github.io/crosstalk/) overcomes this with shared data objects that react to user inputs, altering the content of multiple widgets on the fly. I'll explain how I used Crosstalk to develop a 'pseudo-app' for exploring schools data with the Leaflet (maps), Plotly (charts) and DT (tables) widgets inside the Flexdashboard framework and how I shared it easily with policy-making users as a static HTML file for exploration in the browser.
Note that this material is restricted to **published data only** and **does not reflect or constitute official government policy**.
### How to use
#### Filters
You can:
* select one or more local authorities from the dropdown menu (remove them with your backspace key)
* select one or more Ofsted grades using the checkboxes
* select the phase of education with the checkboxes
* drag the slider to select a pupil count
* drag the slider to filter by the percenatge of pupils receiving free school meals
#### Interactive map
You can:
* click to grab and drag the map around
* zoom with the '+' and '--' buttons (top-left) or with your mouse's scroll wheel
* click a marker to reveal a popup with information about that school
* click the button showing a broken square (top-left under the zoom options) to select points on the map using a window that's draggable (click and hold the grid icon in the upper left) and resizeable (click and drag the white boxes in each corner)
#### Interactive table
You can:
* filter each column by typing in the boxes under each column header
* sort the columns (ascending and descending) by clicking on the column header
* change which columns are visible by clicking the Column visibility button
* click 'CSV' or 'Excel' to download the filtered data to a .csv file or a .xlsx
* see how many entries remain after filtering in the bottom-left, where it says 'Showing X to Y of Z entries'
### Tools
[R v3.4.4](https://www.r-project.org/) and [RStudio v1.1.442](https://www.rstudio.com/) were used to build this tool.
The packages used were:
* [Flexdashboard](https://rmarkdown.rstudio.com/flexdashboard/) to create a frame for the content
* [Leaflet](https://rstudio.github.io/leaflet/) for the interactive map
* [DT](https://rstudio.github.io/DT/) for the interactive table
* [Crosstalk](https://rstudio.github.io/crosstalk/) for widget interactivity
* [Ion icons](https://ionicons.com/) and [Font Awesome](https://fontawesome.com/) for icons
The code for this tool is available from [github.com/matt-dray/earl18-crosstalk](https://github.com/matt-dray/earl18-crosstalk). The presentation is available from [github.com/matt-dray/earl18-presentation](https://github.com/matt-dray/earl18-presentation).