-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
99 lines (89 loc) · 2.35 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
library(tidyverse)
library(leaflet)
library(shiny)
library(glue)
library(sf)
UASCs <- read_csv("UASCs.csv")
adm2 <- st_read("~/augmentedADM2s.geojson", stringsAsFactors = FALSE)
SNCs <- c("Unaccompanied Children" = "UC",
"Separated Children" = "SC")
Genders <- c("Male", "Female")
Ages <- c(0, 18)
CoOs <- unique(UASCs$CoO)
ui <- fillPage(
leafletOutput("map", height = "100%"),
absolutePanel(
top = 20,
right = 20,
height = "100%",
wellPanel(
radioButtons(
"SNC",
"Specific Needs Group",
choices = SNCs,
selected = SNCs[1],
inline = TRUE
),
hr(),
sliderInput("Age", "Age", Ages[1], Ages[2], Ages, step = 1),
hr(),
checkboxGroupInput(
"Gender",
"Gender",
choices = Genders,
selected = Genders,
inline = TRUE
),
hr(),
checkboxGroupInput(
"CoO",
"Country of Origin",
choices = sort(CoOs),
selected = CoOs
)
)
)
)
server <- function(input, output) {
mapData <- reactive({
adm2 %>% left_join(
UASCs %>% filter(
SNC == input$SNC,
Age %>% between(input$Age[1], input$Age[2]),
Gender %in% input$Gender,
CoO %in% input$CoO
) %>%
count(Sec_Code)
) %>% replace_na(list(n = 0)) %>% filter(n > 0)
})
colorPalette <- reactive({
colorNumeric("Blues", NULL)
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles(
urlTemplate = "https://{s}.tile.thunderforest.com/{variant}/{z}/{x}/{y}.png?apikey={apikey}",
attribution = "© <a href='http://www.thunderforest.com/'>Thunderforest</a>, © <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
options = tileOptions(variant = "transport", apikey = Sys.getenv("THUNDERFOREST_API_KEY"))
) %>%
fitBounds(24.7, 22.0, 36.9, 31.7)
})
observe({
pal <- colorPalette()
leafletProxy("map", data = mapData()) %>%
clearShapes() %>%
clearControls() %>%
addPolygons(
color = ~ pal(log10(n)),
label = ~ glue("{n} in {Sec_Name_En}, {Gov_Name_En}."),
fillOpacity = .5
) %>%
addLegend(
position = "bottomleft",
pal = pal,
values = ~ n,
title = glue("# of {input$SNC}")
)
})
}
shinyApp(ui = ui, server = server)