-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.R
More file actions
228 lines (173 loc) · 6.87 KB
/
dashboard.R
File metadata and controls
228 lines (173 loc) · 6.87 KB
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
#load libraries
library(shiny)
library(tidyverse)
library(lubridate)
library(leaflet)
library(here)
library(leaflet.extras)
library(shinyWidgets)
library(leafpop)
library(shinydashboard)
#load data
damdata <- read_csv(here("damdata.csv")) %>%
mutate(purpose = fct_relevel(purpose, c("Hydroelectricity",
"Irrigation",
"Irrigation & Hydroelectricity",
"Irrigation & Water supply")))
damspat <- read_csv(here("damspats.csv")) %>%
mutate(purpose = fct_relevel(purpose, c("Hydroelectricity",
"Irrigation",
"Irrigation & Hydroelectricity",
"Irrigation & Water supply")),
district = as_factor(district))
load(here("popmaps.RData")) #this is called "maps" in the environment
#because that's what it was called in leafpop.Rmd and somehow R knows
#create static pieces
content <- paste("<b>", damspat$reservoir_name, "Dam", "</b></br>",
"River:", damspat$river, "</br>",
"Purpose:", damspat$purpose, "</br>",
"Effective storage capacity:", damspat$effective_storage_capacity_109m3, "BCM")
#CREATE UI
ui <- dashboardPage(
dashboardHeader(title = "Dam dashboard"),
#sidebar
dashboardSidebar(
sidebarMenu(
menuItem("Map", tabName = "maptab", icon = icon("dashboard")),
menuItem("Plot", tabName = "plottab", icon = icon("th")),
menuItem("Info", tabName = "infotab", icon = icon("th"))
)
),
#body
dashboardBody(
tabItems(
#map tab
tabItem(tabName = "maptab",
fluidPage(
box(
title = "Maharashtra Dams",
leafletOutput("map", height = 500, width = 700)
)
)
),
#plot tab
tabItem(tabName = "plottab",
fluidRow(
#the inputs for the plot
box(width = 3,
checkboxGroupInput(inputId = "damtype",
label = "Dam use",
choices = levels(damdata$purpose),
selected = levels(damdata$purpose)),
pickerInput(inputId = "damdist",
label = "District",
choices = unique(damdata$district),
selected = unique(damdata$district),
options = list(`actions-box` = TRUE,
size = 10,
`selected-text-format` = "count > 3"
),
multiple = TRUE),
dateRangeInput(inputId = "dates",
label = "Date range",
start = as_date("2015-01-01"), end = as_date("2016-12-01"),
min = as_date("2015-01-01"), max = as_date("2020-12-01")),
textOutput("dateinfo")
),
#the actual plot
box(width = 6,
title = h3("Water storage over time"),
plotOutput("plot", width = 600)
)
)
),
#info tab
tabItem(tabName = "infotab",
uiOutput("credit"), uiOutput("link"), uiOutput("link2")
)
)
)
)
#create server function
server <- function(input, output) {
#map
output$map <- renderLeaflet({
leaflet(options = leafletOptions(minZoom = 3, maxZoom = 9)) %>%
addProviderTiles(providers$CartoDB.VoyagerLabelsUnder) %>%
setView(lat = 19.2, lng = 76.1, zoom = 7) %>%
setMaxBounds(lat1 = 10, lng1 = 62.2,
lat2 = 29, lng2 = 90.2) %>%
addCircleMarkers(data = damspat,
lat = ~lat, lng = ~long,
stroke = FALSE, fillOpacity = 0.65,
radius = ~effective_storage_capacity_109m3*10,
group = "Depletion.Curve",
label = unique(damspat$reservoir_name)) %>%
addPopupGraphs(maps, group = "Depletion.Curve", width = 400, height = 300) %>%
addCircleMarkers(data = damspat,
lat = ~lat, lng = ~long,
stroke = FALSE, fillOpacity = 0.65,
radius = ~effective_storage_capacity_109m3*10,
group = "Text.Info",
label = unique(damspat$reservoir_name),
popup = content) %>%
addLayersControl(baseGroups = c("Depletion.Curve", "Text.Info"),
options = layersControlOptions(collapsed = FALSE),
position = "topright") %>%
htmlwidgets::onRender("
function() {
$('.leaflet-control-layers-list').prepend('<label style=\"text-align:center\"><u>Toggle Popups</u></label>');
}
")
})
#plot
damreact <- reactive({
damdata %>%
filter(purpose %in% input$damtype,
district %in% input$damdist,
date <= input$dates[2], date >= input$dates[1])
})
output$plot <- renderPlot({
ggplot(damreact(),
aes(x = date, y = storage_bcm, color = purpose, group = reservoir_name)) +
geom_point(alpha = 0.6,
size = damreact()$effective_storage_capacity_109m3*2) +
geom_line(alpha = 0.2, color = "black") +
theme_minimal() +
labs(y = bquote("Water storage"~(10^9~m^3)), color = "Use",
size = bquote("Effective storage capacity"~(10^9~m^3))) +
theme(axis.title.x = element_blank()) +
guides(color = guide_legend(order = 1),
size = guide_legend(order = 2)) +
scale_color_manual(breaks = c("Hydroelectricity",
"Irrigation",
"Irrigation & Hydroelectricity",
"Irrigation & Water supply"),
values = c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3"))
})
output$dateinfo <- renderText({
"The data are at the monthly level and range from January 2015 to December 2020.
We recommend subsetting the data to avoid overplotting."
})
#credits
output$credit <- renderUI({
auth <- "Authors:"
ad <- "Aditya Gadkari"
la <- "Lauren Rabe"
skip <- " "
sc <- "Sources:"
HTML(paste(auth, ad, la, skip, sc, sep = "</br>"))
})
output$link <- renderUI({
tags$a(href = "https://indiawris.gov.in/wris/#/",
target = "_blank",
"India Water Resources Information System")
})
output$link2 <- renderUI({
tags$a(href="https://en.wikipedia.org/wiki/List_of_dams_and_reservoirs_in_Maharashtra",
target = "_blank",
"Wikipedia: List of dams and reservoirs in Maharashtra")
})
}
#create dashboard
shinyApp(ui, server)