-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractiveMap.R
114 lines (106 loc) · 3.01 KB
/
InteractiveMap.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
library(shiny)
library(leaflet)
library(readxl)
house <- read_excel("Real estate valuation data set.xlsx")
ui <- fluidPage(
leafletOutput("map")
)
# Define color palettes based on the range of values
PricePalette <- colorNumeric(
palette = c("blue", "red"),
domain = house$`Y house price of unit area`
)
AgePalette <- colorNumeric(
palette = c("blue", "red"),
domain = house$`X2 house age`
)
MRTPalette <- colorNumeric(
palette = c("blue", "red"),
domain = house$`X3 distance to the nearest MRT station`
)
StorePalette <- colorNumeric(
palette = c("blue", "red"),
domain = house$`X4 number of convenience stores`
)
server <- function(input, output, session) {
# Render the map
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
addCircles(
data = house,
lng = ~ `X6 longitude`,
lat = ~ `X5 latitude`,
color = ~ PricePalette(`Y house price of unit area`),
group = "House Price"
) %>%
addCircles(
data = house,
lng = ~ `X6 longitude`,
lat = ~ `X5 latitude`,
color = ~ AgePalette(`X2 house age`),
group = "House Age"
) %>%
addCircles(
data = house,
lng = ~ `X6 longitude`,
lat = ~ `X5 latitude`,
color = ~ MRTPalette(`X3 distance to the nearest MRT station`),
group = "MRT Stations" # Updated label here
) %>%
addCircles(
data = house,
lng = ~ `X6 longitude`,
lat = ~ `X5 latitude`,
color = ~ StorePalette(`X4 number of convenience stores`),
group = "Convenience Stores"
) %>%
addLayersControl(
baseGroups = c(
"House Price",
"House Age",
"MRT Stations",
"Convenience Stores"
),
position = "bottomleft",
options = layersControlOptions(collapsed = FALSE)
) %>%
addLegend(
"topright",
pal = PricePalette,
values = house$`Y house price of unit area`,
group = "House Price",
title = "House Price (unit)"
)
})
# Update legend when the selected layer group changes
observeEvent(input$map_groups, {
leafletProxy("map") %>% clearControls() %>%
addLegend(
"topright",
pal = switch(
input$map_groups,
"House Price" = PricePalette,
"House Age" = AgePalette,
"MRT Stations"= MRTPalette,
StorePalette
),
values = switch(
input$map_groups,
"House Price" = house$`Y house price of unit area`,
"House Age" = house$`X2 house age`,
"MRT Stations"= house$`X3 distance to the nearest MRT station`,
house$`X4 number of convenience stores`
),
group = input$map_groups,
title = switch(
input$map_groups,
"House Price" = "Price per Unit Area",
"House Age" = "Year",
"MRT Stations"="Distance(Meter)",
"Number"
)
)
})
}
shinyApp(ui, server)