-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
267 lines (221 loc) · 7.82 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
#
# This is a Shiny web application
# Made by Lucca Nielsen
#
# Github Project:https://github.com/Luccan97/StandardizedMortality_Brazil
# Packages
library(shiny)
library(shinydashboardPlus)
library(shinyWidgets)
library(leaflet)
library(tidyverse)
library(sf)
library(bslib)
# Reading the cleaned datasets from github repo
# deaths
obts_clean <- read_csv("https://raw.githubusercontent.com/Luccan97/StandardizedMortality_Brazil/main/data/obts_clean.csv",
col_types = cols(Sex = col_character()),
locale = locale(encoding = "ISO-8859-1"))
# The shp file needs a diferent process, we need to download them first...
githubURL <- ("https://raw.githubusercontent.com/Luccan97/StandardizedMortality_Brazil/main/data/UF_shp.RDS")
download.file(githubURL,"UF_shp.rds", method="curl")
UF_shp <- readRDS("UF_shp.rds")
# population
pop_t_clean <- read_csv("https://raw.githubusercontent.com/Luccan97/StandardizedMortality_Brazil/main/data/pop_t_clean.csv")
# standard population
standard_pop_clean <- read_csv("https://raw.githubusercontent.com/Luccan97/StandardizedMortality_Brazil/main/data/standard_pop_clean.csv")
ui <- fluidPage(
# overall theme
theme = bs_theme(bootswatch = 'yeti'),
h1(id="tag1", "Open data <> Free knowledge <> Public Health."),
h1(id="tag2", "Standardized mortality rate by sex and age group in the States of Brazil (2010-2019)."),
tags$head(
tags$style(HTML(
# CSS styling some features
"#
plot-container {
position: relative;
}
#loading-spinner {
position: absolute;
left: 50%;
top: 50%;
z-index: -1;
margin-top: -33px; /* half of the spinner's height */
margin-left: -33px; /* half of the spinner's width */
}
#plot.recalculating {
z-index: -2;
}
#tag1 {color: white;
background-color:#405d27;
border:2px solid #c1946a;
border-radius: 15px 50px 30px;
font-size: 20px;
font-style: bold;
text-align:right;
padding-top:10px;
padding-bottom:10px;
}
#tag2 {color: #c1946a;
background-color: #405d27;
border-radius: 15px 50px 30px;
border:2px solid #c1946a;
font-size: 25px;
font-style: bold;
text-align:center;
padding-top:10px;
padding-bottom:10px;
}
.tabbable > .nav > li > a[data-value='Map']
{background-color: #405d27;
border:2px solid #c1946a;
color:#c1946a;
width: 8vw;
text-align:center;
font-style:bold;
font-size:20px;
}
.tabbable > .nav > li > a[data-value='Data']
{background-color: #405d27;
border:2px solid #c1946a;
color:#c1946a;
width: 8vw;
text-align:center;
font-size:20px;
}
.tabbable > .nav > li > a[data-value='README']
{background-color: white;
border:2px solid #c1946a;
color:#c1946a;
width: 8vw;
text-align:center;
font-style:bold;
font-size:20px;
}
"))),
# Sidebar with brief description of dashboard utilities and input choices
sidebarLayout(
sidebarPanel(
width = 6,
h4("What is it and how to use it?"),
helpText("The dashboard offers an interactive visualization of the spatial distribution in the Federative Units of Brazil of the mortality rates standardized by sex and age group,
according to the basic causes grouped in the chapters of the ICD-10.
To learn more about the build process, click on the 'READme' tab of the panel.
To create a specific map:
Select a year, rate type (standardized or crude), and the ICD-10 chapter of interest."),
selectInput("year",
"Year:",
selected = "2019",
choices = c('2010','2011','2012','2013','2014','2015','2016','2017','2018','2019')),
selectInput("taxa",
"Rate:",
choices = c('Standardized', 'Crude')),
prettyRadioButtons("cid",
'Basic Cause by Chapter (ICD-10):',
choices = unique(obts_clean$ICD_chapter),
shape = "square",
status = 'warning',
bigger = T,
animation = "smooth")),
mainPanel(width = 6,
tabsetPanel(
tabPanel("Map",
div(
id = "plot-container",
tags$img(src = "spinner.gif",
id = "loading-spinner"),
leafletOutput("map1",
width = "100%",
height = "900px"))),
tabPanel("Data",
tableOutput("table1")),
tabPanel("README",
includeMarkdown("README.md"))
)
)
))
# Define server logic
server <- function(input, output) {
obts1 <- reactive({
obts_clean %>%
filter(Year == input$year & ICD_chapter == input$cid)
})
pop_t1 <- reactive(
pop_t_clean %>%
filter(Year == input$year)
)
df1 <- reactive(
right_join(obts1(), pop_t1(), by = c('Sex', 'agegroup', 'UF'))
)
df2 <- reactive(
left_join(df1(), standard_pop_clean, by = c('Sex', 'agegroup'))
)
# Calculating standarized rates based on R Epi handbook lesson
mortality_ds_rate_phe <-
reactive(
df2() %>%
group_by(UF) %>%
PHEindicatormethods::phe_dsr(
x = Deaths,
n = pop.x,
stdpop = pop.y,
stdpoptype = "field") %>%
mutate(Crude = total_count/total_pop * 100000)%>%
rename(Standardized = value)
)
output$table1 <- renderTable({
mortality_ds_rate_phe() %>%
rename(Obitos = 'total_count',
Populacao = 'total_pop') %>%
select(c(UF,Obitos,Populacao,Crude,Standardized))
})
map <- reactive(
left_join(UF_shp, mortality_ds_rate_phe(), by = c('NM_UF'= 'UF')) %>%
mutate(inc_cat = cut(get(input$taxa), include.lowest = T,
breaks = c(quantile(get(input$taxa), by = 0.2, na.rm = T))))
)
pal <- reactive(
colorFactor(
palette = 'YlOrRd',
domain = map()$inc_cat)
)
labels <- reactive(
paste0("<strong>",map()$NM_UF,"</strong><br/>",
"Standardized Rate: ", round(map()$Standardized,1),"<br/>",
"Crude Rate: ", round(map()$Crude,1)
) %>% lapply(htmltools::HTML)
)
output$map1 <- renderLeaflet({
# Thats out leaflet map
leaflet(map()) %>%
addProviderTiles("MapBox", options = providerTileOptions(
id = "mapbox.light")) %>%
# set view
setView(lng =-52.9500 ,lat = -10.6500, zoom = 4.5) %>%
# Polygons
addPolygons(
fillColor = ~pal()(inc_cat),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
highlightOptions = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
label = labels(),
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
# Legend
addLegend(values = map()$inc_cat,pal = pal(), opacity = 0.7, title = NULL,
position = "topleft")
})
}
# Run the application
shinyApp(ui = ui, server = server)