forked from chao92/WeatherMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
executable file
·475 lines (395 loc) · 13.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
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
library(leaflet)
library(RColorBrewer)
library(scales)
library(lattice)
library(dplyr)
library(darksky)
library(ggplot2)
library(purrr)
suffix = ".rda"
# Leaflet bindings are a bit slow; for now we'll just sample to compensate
set.seed(100)
zipdata <- allzips[sample.int(nrow(allzips), 10000),]
# By ordering by centile, we ensure that the (comparatively rare) SuperZIPs
# will be drawn last and thus be easier to see
zipdata <- zipdata[order(zipdata$centile),]
function(input, output, session) {
## Interactive Map ###########################################
# Create the map
output$map <- renderLeaflet({
leaflet() %>%
addTiles(
urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>'
) %>%
setView(lng = -93.85, lat = 37.45, zoom = 4)
})
# A reactive expression that returns the set of zips that are
# in bounds right now
zipsInBounds <- reactive({
if (is.null(input$map_bounds))
return(zipdata[FALSE,])
bounds <- input$map_bounds
latRng <- range(bounds$north, bounds$south)
lngRng <- range(bounds$east, bounds$west)
subset(zipdata,
latitude >= latRng[1] & latitude <= latRng[2] &
longitude >= lngRng[1] & longitude <= lngRng[2])
})
# Precalculate the breaks we'll need for the two histograms
centileBreaks <- hist(plot = FALSE, allzips$centile, breaks = 20)$breaks
# output$histCentile <- renderPlot({
# # If no zipcodes are in view, don't plot
# if (nrow(zipsInBounds()) == 0)
# return(NULL)
#
# hist(zipsInBounds()$centile,
# breaks = centileBreaks,
# main = "SuperZIP score (visible zips)",
# xlab = "Percentile",
# xlim = range(allzips$centile),
# col = '#00DD00',
# border = 'white')
# })
# output$scatterCollegeIncome <- renderPlot({
# # If no zipcodes are in view, don't plot
# if (nrow(zipsInBounds()) == 0)
# return(NULL)
#
# print(xyplot(income ~ college, data = zipsInBounds(), xlim = range(allzips$college), ylim = range(allzips$income)))
# })
# This observer is responsible for maintaining the circles and legend,
# according to the variables the user has chosen to map to color and size.
observe({
leafletProxy("map", data = zipdata) %>%
clearShapes() %>%
addCircles(~longitude, ~latitude, radius=4000, layerId=~zipcode,
stroke=FALSE, fillOpacity=0.4, fillColor="#03F")
})
# Show a popup at the given location
showZipcodePopup <- function(zipcode, lat, lng) {
selectedZip <- allzips[allzips$zipcode == zipcode,]
content <- as.character(tagList(
#tags$h4("Score:", as.integer(selectedZip$centile)),
tags$strong(HTML(sprintf("%s, %s %s",
selectedZip$city.x, selectedZip$state.x, selectedZip$zipcode
))), tags$br(),
sprintf("Current Weather: %s", now["currently"]$currently$icon), tags$br(),
sprintf("Current Tempreture: %s F", as.integer(now["currently"]$currently$temperature)), tags$br(),
sprintf("Current Wind Speed: %s mph", now["currently"]$currently$windSpeed)
))
leafletProxy("map") %>% addPopups(lng, lat, content, layerId = zipcode)
}
# When map is clicked, anywhere
observe({
leafletProxy("map") %>% clearPopups()
event <- input$map_click
if (is.null(event))
return()
dlng <- event$lng - cleantable$Long
dlat <- event$lat - cleantable$Lat
dd = (dlng ^ 2 + dlat ^ 2)^0.5
index <- which.min(dd)
nid <- cleantable[index,]$"Zipcode"
nlat <- cleantable[index,]$"Lat"
nlng <- cleantable[index,]$"Long"
print("get the nid")
print(nid)
isolate({
showZipcodePopup(nid, nlat, nlng)
})
fname <- paste(nid,suffix,sep="")
if(file.exists(fname)){
# 24 hours expired
if((Sys.time() - file.info(fname)$ctime) < 1440){
load(fname)
now <<- now
print("loading file:")
}else{
now <<- get_current_forecast(nlat, nlng)
save(now,file=fname)
print("file expired:")
}
}else{
now <<- get_current_forecast(nlat, nlng)
save(now,file=fname)
print("no caching file, download:")
}
})
observe({
leafletProxy("map") %>% clearPopups()
event <- input$map_shape_click
if (is.null(event))
return()
# cwang
dlng <- event$lng - cleantable$Long
dlat <- event$lat - cleantable$Lat
dd = (dlng ^ 2 + dlat ^ 2)^0.5
index <- which.min(dd)
nid <- cleantable[index,]$"Zipcode"
nlat <- cleantable[index,]$"Lat"
nlng <- cleantable[index,]$"Long"
print("get closely nid")
print(nid)
fname <- paste(nid,suffix,sep="")
if(file.exists(fname)){
print("file exists")
# 24 hours expired
if((Sys.time() - file.info(fname)$ctime) < 1440){
load(fname)
now <<- now
print("loading file:")
}else{
now <<- get_current_forecast(nlat, nlng)
save(now,file=fname)
print("file expired:")
}
}else{
print("file is not exist")
now <<- get_current_forecast(nlat, nlng)
save(now,file=fname)
print("no caching file, download:")
}
isolate({
showZipcodePopup(nid, nlat, nlng)
})
})
# test query specific date
# if (is.null(input$date))
# return()
# else{
# print("date is")
# print(input$date)
# # specific_tmp = get_forecast_for(43.2672, -70.8617,)
# }
## Data Explorer ###########################################
observe({
cities <- if (is.null(input$states)) character(0) else {
filter(cleantable, State %in% input$states) %>%
`$`('City') %>%
unique() %>%
sort()
}
stillSelected <- isolate(input$cities[input$cities %in% cities])
updateSelectInput(session, "cities", choices = cities,
selected = stillSelected)
})
observe({
zipcodes <- if (is.null(input$states)) character(0) else {
cleantable %>%
filter(State %in% input$states,
is.null(input$cities) | City %in% input$cities) %>%
`$`('Zipcode') %>%
unique() %>%
sort()
}
stillSelected <- isolate(input$zipcodes[input$zipcodes %in% zipcodes])
updateSelectInput(session, "zipcodes", choices = zipcodes,
selected = stillSelected)
# get the zipcodes
print(input$zipcodes)
if (is.null(input$zipcodes))
return()
else{
print(input$zipcodes)
tmp <- cleantable[cleantable$Zipcode == input$zipcodes,]
print("else ")
print(tmp)
x <- tmp$Lat
y <- tmp$Long
print("we have Lat from zipcode")
print(x)
print(y)
check_location <<- get_current_forecast(x,y)
check_location_icon <<- check_location["currently"]$currently$icon
# print(check_location_icon)
srcstr <<- ""
if (check_location_icon == "clear-day") {
srcstr = "./images/1.png"
}
else if (check_location_icon == "clear-night") {
srcstr = "./images/2.png"
}
else if (check_location_icon == "rain") {
srcstr = "./images/3.png"
}
else if (check_location_icon == "snow") {
srcstr = "./images/4.png"
}
else if (check_location_icon == "sleet") {
srcstr = "./images/5.png"
}
else if (check_location_icon == "wind") {
srcstr = "./images/6.png"
}
else if (check_location_icon == "fog") {
srcstr = "./images/7.png"
}
else if (check_location_icon == "cloudy") {
srcstr = "./images/89.png"
}
else if (check_location_icon == "partly-cloudy-day") {
srcstr = "./images/89.png"
}
#check_location_icon == "partly-cloudy-night"
else{
srcstr = "./images/10.png"
}
output$weatherIcon <- renderImage({
print(srcstr)
return(list(
src = srcstr,
filetype = "image/png",
alt = "This is a raining"
))
}, deleteFile = FALSE)
# replot the figure
output$Hourplot <-renderPlot({
# now <- get_current_forecast(37.8267,-122.4233)
now = check_location
print(now)
# output$plotwether = weather_plot(now)
hourlytmp <- now["hourly"]
userfuldata <- hourlytmp$hourly
print(userfuldata)
print("get time")
print(userfuldata["time"][,c(1)])
# hist(rnorm(100))
# x <- as.POSIXct(as.Date(c(userfuldata["time"][,c(1)]),"%Y-%m-%d %H:%M:%S", tz="Europe/London"))
# print("x is")
# print(range(x))
axis(1, at=1:7)
print(plot(temperature ~ time, data = userfuldata, ylim = range(userfuldata["temperature"]), type="b"))
})
output$MinPlot <-renderPlot({
# now <- get_current_forecast(37.8267,-122.4233)
now = check_location
print(now)
# output$plotwether = weather_plot(now)
hourlytmp <- now["minutely"]
userfuldata <- hourlytmp$minutely
print(userfuldata)
print("get time")
print(userfuldata["time"][,c(1)])
# hist(rnorm(100))
# x <- as.POSIXct(as.Date(c(userfuldata["time"][,c(1)]),"%Y-%m-%d %H:%M:%S", tz="Europe/London"))
# print("x is")
# print(range(x))
axis(1, at=1:7)
print(plot(precipIntensity ~ time, data = userfuldata, ylim = range(userfuldata["precipIntensity"]), type="b"))
})
output$Dailyplot <-renderPlot({
# now <- get_current_forecast(37.8267,-122.4233)
now = check_location
print(now)
# output$plotwether = weather_plot(now)
hourlytmp <- now["daily"]
userfuldata <- hourlytmp$daily
print(userfuldata)
print("get time")
print(userfuldata["time"][,c(1)])
# hist(rnorm(100))
# x <- as.POSIXct(as.Date(c(userfuldata["time"][,c(1)]),"%Y-%m-%d %H:%M:%S", tz="Europe/London"))
# print("x is")
# print(range(x))
axis(1, at=1:7)
print(plot(temperatureHigh ~ time, data = userfuldata, ylim = range(userfuldata["temperatureHigh"]), type="b"))
})
# update the tmp
current_tmp = check_location["currently"]$currently$temperature
output$test1 <- renderText({
paste(c(current_tmp, "F"), collapse = "")
})
# check history data of above city
output$hisHourplot <-renderPlot({
if (is.null(input$date))
return()
else{
print("date is")
print(input$date)
specific_tmp = get_forecast_for(x, y,input$date)
print("specific tmp")
histo_tmp = specific_tmp
# print(now)
# output$plotwether = weather_plot(now)
hourlytmp <- histo_tmp["hourly"]
userfuldata <- hourlytmp$hourly
print(userfuldata)
print("get time")
print(userfuldata["time"][,c(1)])
# hist(rnorm(100))
# x <- as.POSIXct(as.Date(c(userfuldata["time"][,c(1)]),"%Y-%m-%d %H:%M:%S", tz="Europe/London"))
# print("x is")
# print(range(x))
axis(1, at=1:7)
print(plot(temperature ~ time, data = userfuldata, ylim = range(userfuldata["temperature"]), type="b"))
}
})
}
})
observe({
if (is.null(input$goto))
return()
isolate({
map <- leafletProxy("map")
map %>% clearPopups()
dist <- 0.5
zip <- input$goto$zip
lat <- input$goto$lat
lng <- input$goto$lng
showZipcodePopup(zip, lat, lng)
print("map")
print(lat)
print(lng)
now <- get_current_forecast(lat, lng)
print(now)
# output$plotwether = weather_plot(now)
hourlytmp <- now["daily"]
userfuldata <- hourlytmp$daily
print(userfuldata)
# print("time is ")
# print(hourlytmp["time"])
# my_xlim <- userfuldata$time(as.POSIXct('2007-08-24 17:30:00', format="%Y-%m-%d %H:%M:%S"))
# output$plotwether <- renderPlot({
# if (nrow(zipsInBounds()) == 0)
# return(NULL)
# print("-------------")
# # print(zipsInBounds())
# # my_xlim <- userfuldata$time(as.POSIXct('2007-08-24 17:30:00', format="%Y-%m-%d %H:%M:%S"))
# # x <- as.POSIXct(as.Date(c("20/01/2001","20/02/2003","21/06/2004"),"%d/%m/%Y"))
# # y <- c(1,2,3)
# # xyplot(y~x)
#
# print(plot(temperatureHigh ~ time, data = userfuldata, ylim = range(userfuldata["temperatureHigh"]), type="b"))
# # print(xyplot(temperatureHigh ~ time, data = userfuldata, ylim = range(userfuldata["temperatureHigh"])))
# })
# has <- intersect(readings, names(x))
# rows <- sapply(has, function(y) nrow(x[[y]]))
# has <- names(which(rows>1))
map %>% fitBounds(lng - dist, lat - dist, lng + dist, lat + dist)
})
})
# output$summary <-renderText({
# print("hello")
# })
output$ziptable <- DT::renderDataTable({
df <- cleantable %>%
filter(
Score >= input$minScore,
Score <= input$maxScore,
is.null(input$states) | State %in% input$states,
is.null(input$cities) | City %in% input$cities,
is.null(input$zipcodes) | Zipcode %in% input$zipcodes
) %>%
mutate(Action = paste('<a class="go-map" href="" data-lat="', Lat, '" data-long="', Long, '" data-zip="', Zipcode, '"><i class="fa fa-crosshairs"></i></a>', sep=""))
action <- DT::dataTableAjax(session, df)
DT::datatable(df, options = list(ajax = list(url = action)), escape = FALSE)
# print(input$zipcodes)
# if(input$zipcodes == ""){
# print("wait")
# }else{
# print("if we alraedy have zipcode")
# print(input$zipcodes)
# }
})
}