-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
220 lines (173 loc) · 6.9 KB
/
server.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
## server, v1 was 348 lines, this meant to be much shorter
server <- function(input, output, session) {
## page switiching function, got this from Wickham's Mastering Shiny book
switch_page <- function(i) {
updateTabsetPanel(session, "user_flow", selected = paste0("tab", i))
}
observeEvent(input$details, switch_page(5))
observeEvent(input$restart, switch_page(1))
observeEvent(input$page_12, switch_page(2))
observeEvent(input$page_21, switch_page(1))
observeEvent(input$page_23, {
req(input$map_click)
switch_page(3)
})
observe({
if(is.null(input$map_click) && input$page_23 > 0)
showModal(modalDialog(
title = "Please select a location on the map first",
"Click the map to choose a 0.5° by 0.5° grid from which satellite data will be downloaded.",
easyClose = TRUE,
footer = NULL
))
})
observeEvent(input$page_34, switch_page(4))
observeEvent(input$page_32, switch_page(2))
observeEvent(input$page_31, switch_page(1))
observeEvent(input$page_42, switch_page(2))
observeEvent(input$page_41, switch_page(1))
observeEvent(input$page_45, switch_page(5))
observeEvent(input$page_15, switch_page(5))
observeEvent(input$page_51, switch_page(1))
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$OpenStreetMap,
options = providerTileOptions(noWrap = FALSE),
group = "Open Street Map (default)") %>%
addProviderTiles(providers$Esri.WorldImagery, group = "ESRI Imagery") %>%
addProviderTiles(providers$Esri.WorldTopoMap, group = "ESRI Topo") %>%
## addProviderTiles(providers$Stamen.TonerLite, group = "Stamen TonerLite") %>%
addLayersControl(
baseGroups = c("Open Street Map (default)", "ESRI Imagery", "ESRI Topo"),
options = layersControlOptions(collapsed = TRUE)) %>%
addSearchOSM() %>%
setView(lng = 100, lat= 13.44, zoom = 5)
})
## Zoom in on user location if given
observe({
if(!is.null(input$lat)) {
map <- leafletProxy("map")
dist <- 1
lat <- input$lat
lng <- input$long
map %>% fitBounds(lng - dist, lat - dist, lng + dist, lat + dist)
}
})
## find lat lon of user selected point on the map
## and place a transparent rectangle over it with 0.5 degree lat lon boundaries
observeEvent(input$map_click, {
click <- (input$map_click)
## adjust the longitude when user scrolls beyond +/- 180
while (click$lng < -180) {
click$lng <- click$lng + 360
}
while (click$lng > 180) {
click$lng <- click$lng - 360
}
## label as north or south or east or west for chart output
north_south <- ifelse(click$lat > 0, "N", "S")
east_west <- ifelse(click$lng > 0, "E", "W")
output$text_location <- renderText(paste("You have selected a point at ",
formatC(abs(click$lat), digits = 1, format = "f"), "° ", north_south,
" and ",
formatC(abs(click$lng), digits = 1, format = "f"), "° ", east_west, ".", sep = ""))
text <- paste(formatC(abs(click$lat), digits = 1, format = "f"), "° ", north_south,
" & ",
formatC(abs(click$lng), digits = 1, format = "f"), "° ", east_west, sep = "")
proxy <- leafletProxy("map")
proxy %>% clearPopups() %>%
clearShapes() %>%
addPopups(click$lng, click$lat, text) %>%
## add here the half-degree boundary box overlay
addRectangles(lng1 = round_any(click$lng, 0.5, floor),
lng2 = round_any(click$lng, 0.5, ceiling),
lat1 = round_any(click$lat, 0.5, floor),
lat2 = round_any(click$lat, 0.5, ceiling),
fill = TRUE, fillColor = "orange", fillOpacity = 0.5,
weight = 2, color = "#3f7300") %>%
addLayersControl(
baseGroups = c("Open Street Map (default)", "ESRI Imagery", "ESRI Topo"),
options = layersControlOptions(collapsed = TRUE)
)
}
)
## a map to use as png in my output file
map_reactive <- reactive({
click <- location()
leaflet(height = 540, width = 540) %>%
setView(lng = click[2], lat = click[1], zoom = 9) %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
addRectangles(lng1 = round_any(click[2], 0.5, floor),
lng2 = round_any(click[2], 0.5, ceiling),
lat1 = round_any(click[1], 0.5, floor),
lat2 = round_any(click[1], 0.5, ceiling),
fill = TRUE, fillColor = "orange", fillOpacity = 0.5,
weight = 2, color = "#3f7300")
})
location <- reactive(
map_click_loc(input$map_click)
)
## get a year of Rs from the location clicked on map
dliDataGet <- eventReactive(input$page_23, {
req(input$map_click)
rs_get(input$map_click)
}
)
## make a plot of a year of daily data, based on that point click
output$dliChart1 <- renderPlot(
make_chart_1(dliDataGet()), res = 96
)
output$click_loc_test <- renderTable( map_click_loc(input$map_click) )
dliNormalGet <- eventReactive(input$page_34, {
rs_normal_get(location())
}
)
output$dliChart2 <- renderPlot(
make_chart_2(dliNormalGet(), dliDataGet()), res = 96
)
map_get <- eventReactive(input$user_flow == 'tab4', {
req(input$user_flow == 'tab4')
# id4 <- showNotification("Getting location image for chart output",
# duration = NULL, closeButton = FALSE, type = "warning")
# on.exit(removeNotification(id4), add = TRUE)
# png_fl = tempfile(fileext = ".png")
#
# mapshot2(map_reactive(),
# file = png_fl)
# vwidth = 540,
# vheight = 540,
# file = "temp.png")
## img.background <- readPNG("temp.png", native = TRUE)
## img.background <- readPNG(png_fl,
## native = TRUE)
## filename <- "temp.png"
})
dirNS <- eventReactive(input$page_23, {
north_south(input$map_click)
}
)
dirEW <- eventReactive(input$page_23, {
east_west(input$map_click)
}
)
output$dli_chart_3pane <- downloadHandler(
filename = function(){
paste("dli_lat",
round(abs(location()[1])),
ifelse(location()[1] > 0, "N", "S"),
"_lon",
round(abs(location()[2])),
ifelse(location()[2] > 0, "E", "W"),
".png", sep = "")
},
content = function(file){
save_plot(file, plot = (
make_chart_1( dliDataGet()) |
(make_chart_2(dliNormalGet(), dliDataGet()) / make_chart_3(map_get()))
) +
plot_layout(nrow = 1, widths = c(3, 1)) +
plot_annotation(theme = theme(plot.background = element_rect(fill = "#f7ffed", color = NA))),
base_asp = 1.78, base_height = 1.8, scale = 4)
}
)
}