Update app.R
Browse files
app.R
CHANGED
|
@@ -9,7 +9,7 @@ library(sf)
|
|
| 9 |
library(rnaturalearth)
|
| 10 |
library(rnaturalearthdata)
|
| 11 |
library(countrycode)
|
| 12 |
-
library(leaflet) # <--
|
| 13 |
|
| 14 |
# =============================
|
| 15 |
# UI
|
|
@@ -321,7 +321,7 @@ server <- function(input, output, session) {
|
|
| 321 |
setView(lng = 0, lat = 20, zoom = 2) # A broad global view
|
| 322 |
})
|
| 323 |
|
| 324 |
-
# Observe changes in the chosen index and update polygons
|
| 325 |
observeEvent(input$indexChoice, {
|
| 326 |
|
| 327 |
plot_data <- cartogram_sf()
|
|
@@ -335,8 +335,6 @@ server <- function(input, output, session) {
|
|
| 335 |
)
|
| 336 |
|
| 337 |
# Construct a label/popup-like text
|
| 338 |
-
# (You could use labelOptions, popup, or separate tooltips)
|
| 339 |
-
# We'll just use label here for quick hover info:
|
| 340 |
labels <- sprintf(
|
| 341 |
"<strong>Country:</strong> %s<br/>
|
| 342 |
<strong>%s Index:</strong> %s",
|
|
@@ -346,7 +344,8 @@ server <- function(input, output, session) {
|
|
| 346 |
) %>% lapply(htmltools::HTML)
|
| 347 |
|
| 348 |
leafletProxy("cartogramPlot", data = plot_data) %>%
|
| 349 |
-
clearShapes() %>%
|
|
|
|
| 350 |
addPolygons(
|
| 351 |
fillColor = ~pal(get(index_col)),
|
| 352 |
fillOpacity = 0.7,
|
|
@@ -359,6 +358,14 @@ server <- function(input, output, session) {
|
|
| 359 |
weight = 2,
|
| 360 |
bringToFront = TRUE
|
| 361 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
)
|
| 363 |
}, ignoreNULL = FALSE) # Trigger once on startup too
|
| 364 |
|
|
|
|
| 9 |
library(rnaturalearth)
|
| 10 |
library(rnaturalearthdata)
|
| 11 |
library(countrycode)
|
| 12 |
+
library(leaflet) # <-- For OpenStreetMap-based map
|
| 13 |
|
| 14 |
# =============================
|
| 15 |
# UI
|
|
|
|
| 321 |
setView(lng = 0, lat = 20, zoom = 2) # A broad global view
|
| 322 |
})
|
| 323 |
|
| 324 |
+
# Observe changes in the chosen index and update polygons + legend
|
| 325 |
observeEvent(input$indexChoice, {
|
| 326 |
|
| 327 |
plot_data <- cartogram_sf()
|
|
|
|
| 335 |
)
|
| 336 |
|
| 337 |
# Construct a label/popup-like text
|
|
|
|
|
|
|
| 338 |
labels <- sprintf(
|
| 339 |
"<strong>Country:</strong> %s<br/>
|
| 340 |
<strong>%s Index:</strong> %s",
|
|
|
|
| 344 |
) %>% lapply(htmltools::HTML)
|
| 345 |
|
| 346 |
leafletProxy("cartogramPlot", data = plot_data) %>%
|
| 347 |
+
clearShapes() %>% # clear existing polygons
|
| 348 |
+
clearControls() %>% # clear existing legends
|
| 349 |
addPolygons(
|
| 350 |
fillColor = ~pal(get(index_col)),
|
| 351 |
fillOpacity = 0.7,
|
|
|
|
| 358 |
weight = 2,
|
| 359 |
bringToFront = TRUE
|
| 360 |
)
|
| 361 |
+
) %>%
|
| 362 |
+
addLegend(
|
| 363 |
+
position = "bottomright",
|
| 364 |
+
pal = pal,
|
| 365 |
+
bins = 5,
|
| 366 |
+
values = plot_data[[index_col]],
|
| 367 |
+
title = paste(index_col, "Index"),
|
| 368 |
+
opacity = 1
|
| 369 |
)
|
| 370 |
}, ignoreNULL = FALSE) # Trigger once on startup too
|
| 371 |
|