-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
219 lines (164 loc) · 9.24 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
library(shiny)
library(dplyr)
library(leaflet)
library(data.table)
library(DT)
library(htmlwidgets)
shinyServer(function(input, output) {
# Import Data and clean it
load('01-data_download/03dadesWithROI.RData')
options(scipen=999)
#grouping for plotting
value_labels<-c(-Inf,6,8,9,10,12,Inf)
color_labels<-c('Inferior al 6%','6-8%','8-9%','9-10%','10-12%','Superior al 12%')
dadesSale[,rentabilidad_grupo:= as.character(cut(ROIpct,value_labels, labels=color_labels))] #important que sigui character
#filtering some wrong ones
# lowers10<-(!dadesSale$rentabilidad_grupo %in% c('10-12%'))
# lowers12<-(!dadesSale$rentabilidad_grupo %in% c('Superior al 12%'))
# dadesSale<-dadesSale[c(which(lowers10 & lowers12),which(!lowers10)[1:25], which(!lowers12)[1:25]),]
# create a color palette for category type in the data file
colors_vec<-c('#FF625E','#FFBE65','#F5EEA5', '#D2FDBB', '#77DD76' ,'#0CC078')
pal <- colorFactor(palette = colors_vec,
levels = color_labels)
#filter data appearing on the map
map_dadesSale_base <- dadesSale
#filter data appearing on the map
map_dadesSale_react <- reactive({
dadesSale %>%
filter(price >= max(input$priceMinSelectInput,0,na.rm=T) &
price <= min(input$priceMaxSelectInput,Inf,na.rm=T) &
SuggestedRentalPrice >= max(input$rentMinSelectInput,0,na.rm=T) &
SuggestedRentalPrice <= min(input$rentMaxSelectInput,Inf,na.rm=T)) %>%
filter(if(sum(unlist(input$statusSelectInput) != '')==0) TRUE else (status %in% input$statusSelectInput)) %>% #no check, no filter
filter(if(sum(unlist(input$roiSelectInput) != '')==0) TRUE else (rentabilidad_grupo %in% input$roiSelectInput))
})
# leaflet Sale Data map
output$saleMap <- renderLeaflet({
dadesSaleLeaflet <- map_dadesSale_base
leaflet(dadesSaleLeaflet) %>%
addTiles(urlTemplate = "http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", attribution = 'Google') %>%
addLegend(colors=colors_vec,labels=color_labels, values= ~rentabilidad_grupo,opacity=1, na.label = "Not Available")%>%
setView(lng = 1.747969, lat = 41.912181, zoom=8)
})
#update map
observe({
# update map
dadesSaleLeaflet <- map_dadesSale_react()
leafletProxy('saleMap',data=dadesSaleLeaflet) %>%
clearMarkerClusters() %>% clearMarkers() %>% clearShapes() %>%
addCircles(lng = ~longitude, lat = ~latitude) %>%
addCircleMarkers(data=dadesSaleLeaflet,
lat = ~latitude, lng =~longitude, layerId = ~propertyCode,
radius = 8, popup = ~as.character(cntnt),
fillColor = ~pal(rentabilidad_grupo),
stroke = T, color = "black", opacity=0.25, fillOpacity = 1)%>%
onRender("
function(el, x) {
this.on('popupopen', function(e) {
Shiny.onInputChange('myEvent', 'open');
});
this.on('popupclose', function(e) {
Shiny.onInputChange('myEvent', 'close');
});
}")
})
#display Filtered Referenced Rent data when user selects a property for sale
observeEvent(input$saleMap_marker_click, {
selectedProperty <- input$saleMap_marker_click$id
selectedLatitude <- input$saleMap_marker_click$lat
selectedLongitude <- input$saleMap_marker_click$lng
referencedProperties<- (dadesSale %>%
filter(propertyCode == selectedProperty) %>%
select(ReferencedRentals)) %>% unlist
distanceToProperty<- (dadesSale %>%
filter(propertyCode == selectedProperty) %>%
select(ReferencedRentalsDistance)) %>% unlist
filteredRentData<-dadesRent %>% filter(propertyCode %in% referencedProperties) #filtro rentals
filteredRentData<-filteredRentData[order(match(filteredRentData$propertyCode,referencedProperties)),] #ordeno
filteredRentData$distanceToProperty<-distanceToProperty*1000
#afegir el de referència també
rowSelectedSale<-dadesSale[propertyCode==selectedProperty,]
filteredRentDataOutput<-datatable(filteredRentData[,c('municipality','distanceToProperty','price','priceByArea',
'propertyTypeEsp','size',
'rooms','floorEsp','hasLiftEsp',
'exteriorEsp','statusEsp','url_html')],
filter = 'none',
colnames = c("Localidad","Distancia (m)","Alquiler", "Alquiler/m2",
"Tipo", "m2",
"Habitaciones","Altura","Ascensor",
"Exterior/Interior","Estado", "Enlace"),
options=list(pageLength=-1,
dom='tr',
scrollX=T),
escape=FALSE,
caption = htmltools::tags$caption("Pisos de alquiler parecidos con los que se calcula el alquiler estimado y su rentabilidad asociada.",
style="text-align: center; font-weight: bold; color:black"),
selection = 'none')
#table
output$filteredRentData <-
DT::renderDataTable(filteredRentDataOutput)
#map
# leaflet filtered Rent Data map
output$rentMap <- renderLeaflet({
leaflet(filteredRentData) %>%
addCircles(lng = ~longitude, lat = ~latitude) %>%
addTiles(urlTemplate = "http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", attribution = 'Google') %>%
addCircleMarkers(data=filteredRentData,
lat = ~latitude, lng =~longitude, layerId = ~propertyCode,
radius = 8, popup = ~as.character(cntnt),
fillColor = "#77ccff",
stroke = T, color = "black", opacity=0.7, fillOpacity = 1) %>%
setView(lng = selectedLongitude, lat = selectedLatitude, zoom=10)
})
observe({
# update map
#rbind(filteredRentData,)
leafletProxy('rentMap',data=rowSelectedSale) %>%
addCircles(lng = ~longitude, lat = ~latitude) %>%
addCircleMarkers(data=rowSelectedSale,
lat = ~latitude, lng =~longitude, layerId = ~propertyCode,
radius = 8, popup = ~as.character(cntnt),
fillColor = ~pal(rentabilidad_grupo),
stroke = T, color = "black", opacity=0.25, fillOpacity = 1)
})
})
#####close referenced properties table/map
#get the click eventwhen the user clicks on the map (we want to close the reference properties map/table)
observeEvent(input$saleMap_click, {
output$filteredRentData<-NULL
output$rentMap<-NULL
})
#get the click event when the user clicks on the close popup (we want to close the reference properties map/table)
observeEvent(input$myEvent, {
output$filteredRentData<-NULL
output$rentMap<-NULL
})
##### PESTANYA 2
#display Sale data
saleDataOutput<-datatable(dadesSale[,c('ROIpct','ROIpct_15','price','priceByArea',
'SuggestedRentalPrice','propertyTypeEsp','size',
'rooms','floorEsp','hasLiftEsp',
'exteriorEsp','statusEsp','address','url')],
filter = 'top',
colnames = c("Rentabilidad", 'Rentabilidad (Precio+15%)',"Precio", "Precio/m2",
"Alquiler estimado", "Tipo", "m2",
"Habitaciones","Altura","Ascensor",
"Exterior/Interior","Estado", "Dirección", "Enlace"),
options=list(pageLength=20,
lengthMenu=c(10,20,50),
dom='lptrp'),
selection = 'none')
output$saleData <-
DT::renderDataTable(saleDataOutput)
##### PESTANYA 3
#display Rent data
output$rentData <-DT::renderDataTable(datatable(
dadesRent[,c('price','priceByArea','propertyTypeEsp','size','rooms','floorEsp','hasLiftEsp','exteriorEsp',
'statusEsp','address','url')],filter = 'top',
colnames = c("Alquiler", "Alquiler/m2", "Tipo", "m2","Habitaciones",
"Altura","Ascensor","Exterior/Interior","Estado", "Dirección", "Enlace"),
options=list(pageLength=20,
lengthMenu=c(10,20,50),
dom='lptrp'),
selection = 'none'))
})