-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.R
654 lines (555 loc) · 23.5 KB
/
app.R
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# -------------------------------------------------------------------
# Program : Use of digital technologies in LAC
# Source : IDB survey
# Country : Multiple
# Author : Laura Goyeneche, Consultant
# lauragoy@iadb.org / lgoyenechec5@gmail.com
# Dependency: SCL-SPH
# Repository: dashboard_digital_tools_covid19
# Objective : Shinny app
# -------------------------------------------------------------------
# Basics
rm(list = ls(all.names = T))
# Libraries
# -------------------------------------------------------------------
# Install library
# install.packages("rgdal", repos = "http://R-Forge.R-project.org")
# Define packages
#library("shiny")
library("shinythemes")
library("shinydashboard")
library("shinyWidgets")
library("leaflet")
library("leaflet.extras")
library("leaflet.minicharts")
library("httr")
library("jsonlite")
library("readxl")
library("rgdal")
library("lubridate")
library("data.table")
library("stringi")
library("stringr")
library("dplyr")
library("DT")
library("plotly")
library("aws.s3")
library("dotenv")
load_dot_env(file = ".env")
Sys.setenv(
"AWS_ACCESS_KEY_ID" = Sys.getenv("AWS_ACCESS_KEY_ID"),
"AWS_SECRET_ACCESS_KEY" = Sys.getenv("AWS_SECRET_ACCESS_KEY"),
"AWS_DEFAULT_REGION" = Sys.getenv("AWS_DEFAULT_REGION")
)
# Attach packages
# invisible(suppressMessages(lapply(packages, library, character.only = T)))
# Data
# -------------------------------------------------------------------
# Working directory
scldatalake = Sys.getenv("scldatalake")
recurso = "/Specialized Survey/Survey on the use of digital tools during COVID-19/"
app_ = "data/app/"
path_ = paste0(scldatalake, recurso, app_)
# Digital technologies
# Import data
# 10 countries in LAC
df = s3read_using(FUN = read.csv,
object = paste0(path_,"data-dashboard.csv"),
encoding = 'UTF-8')
df = df %>% mutate(CODE_ = case_when(COUNTRY == "Chile" ~ "CHI",
COUNTRY == "Uruguay" ~ "URY",
COUNTRY == "Peru" ~ "PER",
COUNTRY == "Paraguay" ~ "PRY",
COUNTRY == "Panama" ~ "PAN",
COUNTRY == "Honduras" ~ "HND",
COUNTRY == "Ecuador" ~ "ECU",
COUNTRY == "Costa Rica" ~ "CRI",
COUNTRY == "El Salvador" ~ "SLV",
COUNTRY == "Mexico" ~ "MEX"))
#df = read.csv("./output/data-dashboard.csv", encoding = "UTF-8")
# Indicators
ind = read.csv("./output/module-indicators.csv", encoding = "UTF-8")
# ind = s3read_using(FUN = read.csv,
# object = paste0(path_,'module-indicators.csv'),
# encoding = "UTF-8")
names(ind)[1] = "modulo"
ind =
ind %>%
mutate(modulo = as.character(modulo),
indicador = as.character(indicador),
title = as.character(title))
# Output data
dfOutput = df
# df %>%
# # Reshape dataset
# reshape2::melt(id.vars = c("Ponderador","COUNTRY","P1","P2","P0"),
# variable.name = "var") %>%
#
# # Process `factor expansion`
# mutate(P0 = P0 * Ponderador,
# value = as.numeric(value),
# value = value * Ponderador) %>%
#
# # Collapse by country, age, gender, and variable
# dplyr::group_by(COUNTRY, P1, P2, var) %>%
#
# # Summarize variables
# dplyr::summarize(total_ = sum(value, na.rm = T),
# count_ = sum(P0)) %>%
#
# # Create percentages
# mutate(n = round((total_ / count_) * 100,2)) %>%
#
# # Select variables of interest
# select(COUNTRY, P1, P2, n, var) %>%
#
# # Rename variables
# dplyr::rename(Pais = COUNTRY,
# Edad = P1,
# Sexo = P2,
# variable = var,
# Porcentaje = n) %>%
#
# # Create summary table
# tidyr::pivot_wider(names_from = Pais, values_from = c("Porcentaje")) %>%
#
# # Format data
# as.data.frame() %>%
#
# # Merge with `indicators` dictionary
# plyr::join(ind, by = "variable") %>%
#
# # Remove NAs
# filter(complete.cases(.)) %>%
#
# # Order variables
# dplyr::relocate(modulo, .after = variable) %>%
# dplyr::relocate(indicador, .after = modulo) %>%
#
# # Rename variable
# dplyr::rename(code = variable,
# Title = title) %>%
#
# # Drop variables
# select(-notes, -cat_id, -category_var, -X)
# Import Shapefiles
# Countries in America
sh_america = s3read_using(FUN = readOGR,
object = paste0(scldatalake,'/Specialized Survey/Survey on the use of digital tools during COVID-19/data/app/shp/America.geojson'),
verbose = F)
names(sh_america@data)[1] = "COUNTRY"
# sh_america <- readOGR(dsn = "./raw/shp/Americas.shp",
# layer = 'Americas',
# verbose = F)
# Other
# -------------------------------------------------------------------
# Changes in absolutePanel:
# For both absolute panel the default color of the background is white
# With this code, the background is between white and transparent
absPanel1 = '#panelOpts {background-color: rgba(255,255,255, 0.7); padding: 0 20px 20px 20px;}'
absPanel2 = '#graphsOpts {background-color: rgba(255,255,255, 0.7); padding: 0 20px 20px 20px;}'
# Color Palette
# Generate personlize palette for the choropleth map
cPal = c('#005B5B','#007979','#009797','#17AEAE','#2EB6B6','#45BEBE','#5CC6C6',
'#73CECE','#8BD6D6','#A2DEDE','#B9E6E6','#D0EEEE','#E7F6F6')
# User interface
# -------------------------------------------------------------------
ui = navbarPage(
# Name of the app
"Uso de Tecnologia en ALC",
id = "nav",
# Theme
theme = shinytheme("flatly"),
# Create different tabs
tabPanel(
# Interactive Map & Graphs
#------------------------------------------------------------
"Mapa Interactivo",
# Leaflet map
leafletOutput("map", width = "100%", height = 900),
# Interactive graphs
# Draggable Panel with the graphs
absolutePanel(
# Define options of absolute panel
id = "graphsOpts",
class = "panel panel-default",
draggable = T,
top = 100,
bottom = "auto",
left = "auto",
right = 450,
width = 800, height = "95%",
# Title for absolute panel
h3(strong("Resultados")),
h6(strong("Fuente:"), "Entrevista telefonica, BID"),
br(),
# Interactive plots
# Country & variable figure
plotlyOutput("plot3", height = 400),
br(),
# Estadisticas por genero
absolutePanel(
right = 450,
width = 320,
h5(strong("Genero")),
plotlyOutput("plot1", height = 240)
),
# Estadisticas por grupos de edad
absolutePanel(
right = 70,
width = 320,
h5(strong("Grupos de edad")),
plotlyOutput("plot2", height = 240)
),
# With the following code we're applying the changes in absolutePanel
# The options are in line 103
fluidPage(tags$head(tags$style(HTML(absPanel2))))
),
# Input options
# Draggable Panel with input options
absolutePanel(
# Define options of absolute panel
id = "panelOpts",
class = "panel panel-default",
draggable = T,
top = 100,
bottom = "auto",
left = "auto",
right = 30,
width = 400, height = "95%",
# Title for absolute panel
h3(strong("Filtros")),
# Input options
# Module name
uiOutput("input0"),
# Modulo options
uiOutput("input1"),
br(),
# With the following code we're applying the changes in absolutePanel
# The options are in line 102
fluidPage(tags$head(tags$style(HTML(absPanel1))))
)
),
tabPanel(
# Data explorer
#------------------------------------------------------------
"Data explorer",
# Title
h5(strong(paste("Datos"))),
hr(),
# Data table output
DT::dataTableOutput("table1"),
hr(),
# Download Handler
h5(strong("Download data:")),
downloadButton('downloadData',"Download data"),
hr()
)
)
# Server function
# -------------------------------------------------------------------
server = function(input, output, session) {
# Tab - Interactive Map
# ---------------------------------------------------------------
# Base map
# ---------------------------------------------------------------
output$map = renderLeaflet({
leaflet() %>%
# Set view in area of interest
setView(lng = 23.7, #-38,
lat = -4.5, #-6.4,
zoom = 3) %>%
# Add base maps
addProviderTiles("CartoDB.DarkMatterNoLabels" , group = "World Dark") %>%
addProviderTiles(provider = "Esri.WorldGrayCanvas", group = "World Gray") %>%
# Add polygons for districts in Chicago
addPolygons(data = sh_america,
color = "#878B8E",
fill = F,
weight = 1.5) %>%
# Add layers control
addLayersControl(
baseGroups = c("World Dark","World Gray"),
position = "bottomleft")
})
# Update module indicators
# ---------------------------------------------------------------
# Modulo category
output$input0 = renderUI({
radioButtons("input0",
h5(strong("Modulo:")),
choices = unique(ind$modulo),
selected = "Uso de tecnologia")
})
# Subset indicators dataframe
indlistInput = eventReactive(input$input0,{
if (length(input$input0) > 0) {
ind_ =
ind %>%
#filter(modulo == "Uso de tecnologia") %>%
filter(modulo == as.character(input$input0)) %>%
filter(variable != "")
return(ind_)
}
})
# Output indicators in selected module only
output$input1 = renderUI({
selectInput("input1",
h5(strong("Indicador:")),
choices = unique(indlistInput()$indicador),
selected = "% que usa smartphones",
width = 450)
})
# Subset variable name
indnameInput = eventReactive(input$input1,{
if (length(input$input1) > 0) {
name =
indlistInput() %>%
filter(indicador == as.character(input$input1))
return(name$variable)
}
})
# Subset category name
catnameInput = eventReactive(input$input1, {
if (length(input$input1) > 0) {
name =
indlistInput() %>%
filter(indicador == as.character(input$input1)) %>%
select(category)
name = as.character(name$category)
return(name)
}
})
# subset title name
titnameInput = eventReactive(input$input1, {
if (length(input$input1) > 0) {
name =
indlistInput() %>%
filter(indicador == as.character(input$input1)) %>%
select(title)
name = as.character(name$title)
return(name)
}
})
# Leaflet maps
# ---------------------------------------------------------------
# Data
dfTemp = eventReactive (input$input1, {
# Subset dataset
temp = df %>%
select(COUNTRY, Ponderador, P0, indnameInput()) %>%
dplyr::rename(val = indnameInput()) %>%
mutate(P0 = ifelse(val >= 0, P0, NA),
tot = val * Ponderador,
count_ = P0 * Ponderador) %>%
dplyr::group_by(COUNTRY) %>%
dplyr::summarize(total_ = sum(tot, na.rm = T),
count_ = sum(count_, na.rm = T)) %>%
mutate(n = (total_ / count_) * 100)
return(temp)
})
# Map
observe({
# Merge `temp` data with country shp
map = sh_america
map@data = plyr::join(map@data, dfTemp(), by = "COUNTRY")
# Create bins for choropleth map
dom = map$n
bin = quantile(dom, na.rm = T)
bin = round(bin,1)
# For one of the selections, the number of bins is 1
# bin length has to be at least 2
if (length(bin) == 1) {
bin = c(0, bin)
}
# Create and match color palette with bins
pal = colorBin(rev(cPal), domain = dom, bins = bin, na.color = 'transparent')
# Choropleth map
leafletProxy("map", data = map) %>%
clearGroup(group = "marker") %>%
clearGroup(group = "chloropleth") %>%
clearControls() %>%
addPolygons(stroke = T,
fillOpacity = 0.7,
fillColor =~ pal(dom),
color = "white",
weight = 0.3,
popup =~ paste(COUNTRY, " %:",round(n,1)),
group = "chloropleth") %>%
addLegend(title = '',
pal = pal,
values =~ n,
na.label = '',
position = 'topleft')
})
# Tab - Statistics
# ---------------------------------------------------------------
# Plot 1: Bar plot by gender
# ---------------------------------------------------------------
output$plot1 = renderPlotly({
df %>%
# Subset
select(CODE_, Ponderador, indnameInput(), P0, P2) %>%
dplyr::rename(val = indnameInput()) %>%
mutate(P0 = ifelse(val >= 0, P0, NA),
tot = val * Ponderador,
count_ = P0 * Ponderador) %>%
dplyr::group_by(CODE_, P2) %>%
dplyr::summarize(total_ = sum(tot, na.rm = T),
count_ = sum(count_, na.rm = T)) %>%
mutate(n = (total_ / count_) * 100) %>%
# Figure
plot_ly(x =~ CODE_,
y =~ n,
color =~ P2,
type = 'bar',
alpha = 0.7) %>%
layout(xaxis = list(title = '', tickangle = -90),
yaxis = list(title = '', range = c(0,100)),
plot_bgcolor = 'transparent',
paper_bgcolor = 'transparent',
legend = list(orientation = 'h',
xanchor = 'center',
x = 0.5,
y = 1.2),
margin = list(l = 0, r = 0, b = 0, t = 0, pad = 0))
})
# # Plot 2: Bar plot by age groups
# # ---------------------------------------------------------------
output$plot2 = renderPlotly({
df %>%
# Subset
select(CODE_, Ponderador, indnameInput(), P0, P1) %>%
dplyr::rename(val = indnameInput()) %>%
mutate(P0 = ifelse(val >= 0, P0, NA),
tot = val * Ponderador,
count_ = P0 * Ponderador) %>%
dplyr::group_by(CODE_, P1) %>%
dplyr::summarize(total_ = sum(tot, na.rm = T),
count_ = sum(count_, na.rm = T)) %>%
mutate(n = (total_ / count_) * 100) %>%
# Figure
plot_ly(x =~ CODE_,
y =~ n,
color =~ P1,
type = 'bar',
alpha = 0.7) %>%
layout(xaxis = list(title = '', tickangle = -90),
yaxis = list(title = '', range = c(0,100)),
plot_bgcolor = 'transparent',
paper_bgcolor = 'transparent',
legend = list(orientation = 'h',
xanchor = 'center',
x = 0.5,
y = 1.2),
margin = list(l = 0, r = 0, b = 0, t = 0, pad = 0))
})
# # Plot 3: Bar plot general
# # ---------------------------------------------------------------
output$plot3 = renderPlotly({
if (indnameInput() %in% subset(ind, ind$cat_id == 1)$variable) {
# Subset
temp =
df %>%
select(CODE_, Ponderador, P0, starts_with(catnameInput())) %>%
#select(CODE_, Ponderador, P0, starts_with("P8")) %>%
select(CODE_, Ponderador, P0, ends_with("_cat")) %>%
melt(id = c("CODE_","Ponderador","P0")) %>%
dplyr::rename(category = value) %>%
mutate(category = as.character(category)) %>%
mutate(variable = as.character(variable)) %>%
dplyr::group_by(CODE_, variable, category) %>%
dplyr::summarize(total_ = sum(Ponderador, na.rm = T)) %>%
dplyr::group_by(CODE_,variable) %>%
mutate(count_ = sum(total_, na.rm = T)) %>%
mutate(n = (total_ / count_) * 100) %>%
mutate(category = ifelse(category == "","N/A",category))
# Unique values variable
nvariable = unique(temp$variable)
# Title
#title = ind[ind$category == "P8",]$title
title = ind[ind$category == catnameInput(),]$title
title = unique(title)
title = as.character(title)
title = paste0("<b>",title,"</b>")
title = paste(strwrap(title, width = 90), collapse = "\n")
# Figure
plots = lapply(nvariable, function(i) {
# Subtitle name
subtitle = gsub("_cat","",i)
subtitle = ind[ind$variable == subtitle,]$category_var
subtitle = as.character(subtitle)
temp %>%
mutate(COUNTRY = as.character(CODE_),
variable = as.character(variable)) %>%
filter(variable == i) %>%
plot_ly(x =~ CODE_,
y =~ n,
color =~ category,
type = 'bar',
alpha = 0.7,
showlegend = (i == nvariable[round(length(nvariable)/2)])) %>%
layout(xaxis = list(title = subtitle, tickangle = -90),
yaxis = list(title = '', range = c(0,100)),
plot_bgcolor = 'transparent',
paper_bgcolor = 'transparent',
barmode = 'stack',
legend = list(orientation = 'h',
xanchor = 'center',
x = 0.5,
y = -0.5),
margin = list(l = 0, r = 0, b = 20, t = 90, pad = 0))
})
subplot(plots, nrows = 1, shareY = T, titleX = T) %>% layout(title = list(text = title, y = 1.5, font = list(size = 13)))
} else {
# Title
title = paste0("<b>",titnameInput(),"</b>")
title = paste(strwrap(title, width = 90), collapse = "\n")
df %>%
# Subset
select(CODE_, Ponderador, indnameInput(), P0) %>%
dplyr::rename(val = indnameInput()) %>%
mutate(P0 = ifelse(val >= 0, P0, NA),
tot = val * Ponderador,
count_ = P0 * Ponderador) %>%
dplyr::group_by(CODE_) %>%
dplyr::summarize(total_ = sum(tot, na.rm = T),
count_ = sum(count_, na.rm = T)) %>%
mutate(n = (total_ / count_) * 100) %>%
# Figure
plot_ly(x =~ CODE_,
y =~ n,
type = 'bar',
alpha = 0.7) %>%
layout(xaxis = list(title = '', tickangle = -90),
yaxis = list(title = '', range = c(0,100)),
title = list(text = title, y = 1.5, font = list(size = 13)),
plot_bgcolor = 'transparent',
paper_bgcolor = 'transparent',
legend = list(orientation = 'h',
xanchor = 'center',
x = 0.5,
y = 1.2),
margin = list(l = 0, r = 0, b = 20, t = 90, pad = 0))
}
})
# Tab - Data Explorer
# ---------------------------------------------------------------
# Data table
# ---------------------------------------------------------------
output$table1 = DT::renderDataTable({
DT::datatable(dfOutput,
options = list(pageLength = 10),
rownames = F)
})
# Download
# ---------------------------------------------------------------
output$downloadData = downloadHandler(
filename = function() {paste("data-", Sys.Date(), ".csv", sep = "")},
content = function(file) {write.csv(df,file)}
)
}
# Run app
# -------------------------------------------------------------------
shinyApp(ui = ui, server = server)
# -------------------------------------------------------------------