-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
279 lines (229 loc) · 10.5 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
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(magrittr)
library(shiny)
library(rgdal)
library(data.table)
library(sp)
library(geosphere)
library(DT)
library(dplyr)
library(dtplyr)
library(lubridate)
library(readxl)
library(jsonlite)
library(plyr)
darzeliai <- readRDS("darzeliai.RDS") %>% sort
allkg <- fread("data/istaigos.csv", encoding = "UTF-8")
allkg1 <- data.frame(allkg) %>% filter(LABEL %in% darzeliai) %>%
select(LABEL, GIS_X, GIS_Y) %>% filter(GIS_X != 0)
coordinates(allkg1) <- c("GIS_X", "GIS_Y")
proj4string(allkg1) <- CRS("+init=epsg:3346")
tmp <- spTransform(allkg1, CRS("+proj=longlat +datum=WGS84"))
#apply(tmp@data, 1, function(x, ))
seniunija <- fread("data/seniunijos.csv", encoding = "UTF-8")
d <- read.csv(file = "data/priority_from_json.csv",
check.names = FALSE, stringsAsFactors = FALSE)
gat <- read.csv(file = "data/GROUP_AGE_TYPE.csv",
sep = ";", encoding = "UTF-8", na.strings = c("", "NA"),
check.names = FALSE, stringsAsFactors = FALSE)
laukiantys <- read.csv(file = "data/laukianciuju_eileje_ataskaita.csv",
sep = ";", encoding = "UTF-8", na.strings = c("", "NA"),
check.names = FALSE, stringsAsFactors = FALSE)
# this input comes from karolis&raminta
empty.slots <- read.csv(file = "data/vietos.csv",
sep = ";", encoding = "UTF-8", na.strings = c("", "NA"),
check.names = FALSE, stringsAsFactors = FALSE)
d <- d[,c("SCH","GLOBALID","GROUPTYPE","ID","ALGORITHM_ID","SCH_ORDERING",
"PRIORITY_SUM","ROWNUM","BIRTHDATE","FINAL_PRIORITY")]
# adding group age type info to main dataset
d <- merge(d, gat[,c("ID","AGEFROM","AGETO")],
by.x = "GROUPTYPE",
by.y = "ID", all.x = T)
d$BIRTHDATE <- date(d$BIRTHDATE)
# adding birth date
# rename.matrix.laukiantys <- read.csv(file = "rename_matrix_laukiantys.csv", check.names = F, stringsAsFactors = F)
# names(laukiantys) <- rename.matrix.laukiantys$changed.names
# laukiantys$born.date <- as.Date(laukiantys$born.date)
d$age.of.child <- ((date("2017-01-01") - d$BIRTHDATE) / 365) %>% floor %>% as.numeric
d$is.from.1.to.3 <- ifelse(d$age.of.child < 3,"below.3","more.3")
empty.slots <- ddply(empty.slots, ~ SCH, function(xframe) {
return(apply(xframe[,-1:-3],2,sum))
})
# names(empty.slots)[6] <- "kg_name"
d <- merge(d, empty.slots, all.x = T)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
rv <- reactiveValues()
observeEvent(input$go, {
if (!input$disclaimer) {
# insertUI("#modal", where = "afterBegin",
# ui = )
showModal(modalDialog(
title = NULL,
"Paspauskite sutikimą šalia mygtuko prognozuoti!",
footer = modalButton("Uždaryti")
))
}
req(input$disclaimer)
output$table <- DT::renderDataTable({
prio <- input$school + input$threemore +
input$unable + input$lonely# + (length(input$otherkids) > 0)
if(input$city == "2") {
prio <- prio + 1e2
} else if (input$city == "1") {
prio <- prio + 1e3
}
input.age.of.child <- (Sys.Date() - input$birthdate) %>%
as.integer %>% `%/%`(365)
# req(rv$home)
# req(rv$work)
# leaving d columns that we need
output$priority <- renderUI({
p(strong("Jūsų prioritetas:"), prio)
})
output$disclaimer <- renderUI({
disclaim <- 'Žemiau pateikti rezultatai galioja su sąlyga, kad darželis bus nurodytas 1 prioritetu. Registruojantis į eilę, rekomenduojame 1 prioritetu nurodyti darželį, į kurį turite didžiausias galimybes pakliūti pagal žemiau pateiktus rezultatus. Jeigu į jį nepateksite, pakliūti į 2 ir žemesniais prioritetais nurodytus darželius tikimybė bus kur kas mažesnė.'
div(disclaim, class="warning-disclaimer")
})
input.add <- allkg %>% select(SCH = ID, elderate = ELDERATE_ID) %>%
mutate(add = ifelse(elderate %in% input$district, 1, 0) +
ifelse(SCH %in% input$otherkg, 1, 0)) %>%
select(SCH, add) %>% data.frame
input.priority <- prio
input.birth.date <- input$birthdate
input.age.type <- ifelse(input.age.of.child <=3,"below.3","more.3")
# sub.d <- subset(d, input.age.of.child >= AGEFROM & input.age.of.child <= AGETO)
# estimate rank number in each kg x group
rank.enroll <- ddply(d[d$SCH_ORDERING == 1 & d$is.from.1.to.3 == input.age.type,], ~ SCH, function(xframe) {
xframe <<- xframe
# cat(paste("Ranking in: SCH -", xframe$SCH[1], sep = " "),"\n")
from.which.to.remove <- ifelse(input.age.type == "below.3","slots.from.1.to.3","slots.from.3.to.inf")
free.space <- xframe[1,from.which.to.remove]
total.space <- xframe[1,paste0("all.",gsub("slots.","",from.which.to.remove))]
add.to.prior <- input.add[input.add$SCH == xframe$SCH[1], 2]
# if (free.space == 0 | is.na(free.space)) {
# print("No space left")
# } else {
# filling birth dates, where missing
# if(any(!is.na(xframe$BIRTHDATE))) {
# xframe[is.na(xframe$BIRTHDATE),"BIRTHDATE"] <- max(xframe$BIRTHDATE,na.rm = T)
# } else {
# xframe$born.date <- input.birth.date # if there are kg and group, where all childs birth dates are missing
# }
# rank.frame <- rbind(data.frame(xframe[,c("FINAL_PRIORITY","BIRTHDATE")],target = 0),
# data.frame(FINAL_PRIORITY = input.priority,BIRTHDATE = input.birth.date, target = 1))
rank.frame <- rbind(data.frame(xframe[,c("FINAL_PRIORITY","BIRTHDATE")],target = 0),
data.frame(FINAL_PRIORITY = input.priority + add.to.prior,BIRTHDATE = input.birth.date, target = 1))
rank.frame <- arrange(rank.frame, desc(FINAL_PRIORITY), BIRTHDATE) # after priority, youngest get first in a queue
place.in.queue <- which(rank.frame$target == 1)
enrolled.or.not <- ifelse(place.in.queue <= free.space, "Yes","No")
# output
output <- c("enrolled.or.not" = enrolled.or.not,
"place.in.queue" = place.in.queue,
"free.slots" = free.space,
"total.slots" = total.space,
"total.in.queue.with.first.priority" = nrow(rank.frame),
"percentile.in.free.slots" = place.in.queue / free.space)
return(output)
# }
})
zz <- arrange(rank.enroll, desc(enrolled.or.not),place.in.queue,desc(free.slots))
if (!is.null(rv$work) & !is.null(rv$home)) {
zz <- zz %>% filter(enrolled.or.not == "Yes") %>%
select(SCH, place.in.queue, free.slots,
total.in.queue.with.first.priority,
total.slots) %>%
left_join(allkg %>% select(ID, LABEL, GIS_X, GIS_Y, BUILDDATE,
`LEFT(ADDRESS, 256)`),
by = c("SCH" = "ID")) #%>% slice(1:20)
coordinates(zz) <- c("GIS_X", "GIS_Y")
proj4string(zz) <- CRS("+init=epsg:3346")
tmp <- spTransform(zz, CRS("+proj=longlat +datum=WGS84"))
kgs <- data.frame(
Darzelis = tmp@data$LABEL,
Eile = tmp@data$place.in.queue,
`Viso eileje` = tmp@data$total.in.queue.with.first.priority,
laisvos = tmp@data$free.slots,
total = tmp@data$total.slots,
`Statybos metai` = tmp@data$BUILDDATE,
Adresas = tmp@data$`LEFT(ADDRESS, 256)`,
Namai = round(distVincentyEllipsoid(tmp, rv$home)/1e3, 1),
Darbas = round(distVincentyEllipsoid(tmp, rv$work)/1e3, 1)
)
names(kgs) <- c("Darželis", "Jūsų vieta eilėje", "Iš viso eilėje laukia","Iš viso laisvų vietų",
"Iš viso vietų", "Statybos metai","Adresas", "Atstumas nuo namų", "Atstumas nuo darbo")
removeUI("#no_work_home")
} else {
output$no_work_home <- renderUI({
div("Neįvedėte namų arba darbo adreso, todėl negalime apskaičiuoti atstumų iki darželių")
})
zz <- zz %>% filter(enrolled.or.not == "Yes") %>%
select(SCH, place.in.queue, free.slots,
total.in.queue.with.first.priority,
total.slots) %>%
left_join(allkg %>% select(ID, LABEL, BUILDDATE,
`LEFT(ADDRESS, 256)`),
by = c("SCH" = "ID"))
kgs <- data.frame(
Darzelis = zz$LABEL,
Eile = zz$place.in.queue,
`Viso eileje` = zz$total.in.queue.with.first.priority,
laisvos = zz$free.slots,
total = zz$total.slots,
`Statybos metai` = zz$BUILDDATE,
Adresas = zz$`LEFT(ADDRESS, 256)`
)
names(kgs) <- c("Darželis", "Jūsų vieta eilėje", "Iš viso eilėje laukia","Iš viso laisvų vietų",
"Iš viso vietų", "Statybos metai","Adresas")
}
DT::datatable(kgs, options = list(
language = list(
search = "Ieškoti",
lengthMenu = "Rodyti _MENU_ įrašų",
paginate = list(
first = "Pirmas",
`next` = "Kitas",
previous = "Ankstesnis",
last = "Paskutinis"
),
info = "Rodomi įrašai nuo _START_ iki _END_ iš _TOTAL_"
)
))
})
})
output$google_maps_API <- renderUI({
connection_info <- readLines('private/google-api-key.csv')
HTML(paste0(
'<script async defer src=" https://maps.googleapis.com/maps/api/js?key=',
connection_info,'&libraries=places&callback=initMap"></script>'
)
)
})
observeEvent(input$marker_lat, {
req(input$marker_lat)
req(input$marker_id == "home-map")
rv$home <- c(input$marker_lng, input$marker_lat)
})
observeEvent(input$marker_lat, {
req(input$marker_lat)
req(input$marker_id == "work-map")
rv$work <- c(input$marker_lng, input$marker_lat)
})
# output$coords <- renderUI({
# req(input$marker_id, input$marker_lat, input$marker_lng)
# tagList(
# div(input$marker_id),
# div(input$marker_lat),
# div(input$marker_lng)
# )
#
# #browser()
# })
})