Leafdown.Rd
This class acts as a wrapper around a leafdown map.
curr_sel_data
A reactiveValue
containing a data.frame with
the metadata and (if available) the corresponding values of all currently selected shapes.
curr_data
The metadata and (if available) the corresponding values of all currently displayed shapes.
curr_map_level
Index of the current map level.
This corresponds to the position of the shapes in the spdfs_list
.
(i.e The highest-level is 1, the next is 2 and so on...).
curr_poly_ids
The ids of all polygons of the current map level.
new()
Initializes the leafdown object.
Leafdown$new(spdfs_list, map_output_id, input, join_map_levels_by = NULL)
spdfs_list
A list with the spdfs of all map levels. This cannot be changed later.
map_output_id
The id from the shiny-ui used in the leafletOutput("<<id>>")
.
Used to observe for _shape_click events.
input
The input
from the shiny app.
join_map_levels_by
A named vector of length length(spdfs_list) - 1
with the columns by which the map
levels should be joined. The first element defines how the first and second map levels should be joined, the second
element does the same for the second and third map levels and so on. The name of an element defines the name of
the join column in the upper map level and the actual element the join column of the lower map level.
By default this is set to c("GID_0" = "GID_0", "GID_1" = "GID_1", ..., "GID_n" = "GID_n"),
where n is length(spdfs_list) - 1
.
draw_leafdown()
Draws the leaflet map on the current map level. All unselected parents will be drawn in gray.
...
Additional arguments given to leaflet::addPolygons
add_data()
Adds the data to the currently displayed shapes. This includes the meta-data AND the values to be visualized in the map.
drill_down()
Drills down to the lower level if:
there is a lower level (for now there are only two levels)
at least one shape is selected to drill down on
This will not redraw the map. Also call add_data
to add data for the new level and then
draw_leafdown
to redraw the map on the new level.
drill_up()
Drills up to the higher level if:
there is a higher level (for now there are only two levels)
This will not redraw the map. Also call add_data
to add data for the new level and then
draw_leafdown
to redraw the map on the new level.
toggle_shape_select()
Selects the shape with the given shape id, or unselects it if it was already selected.
if (FALSE) {
library(leafdown)
library(leaflet)
library(shiny)
library(dplyr)
library(shinyjs)
ger1 <- raster::getData(country = "Germany", level = 1)
ger2 <- raster::getData(country = "Germany", level = 2)
spdfs_list <- list(ger1, ger2)
ui <- shiny::fluidPage(
useShinyjs(),
actionButton("drill_down", "Drill Down"),
actionButton("drill_up", "Drill Up"),
leafletOutput("leafdown")
)
server <- function(input, output) {
my_leafdown <- Leafdown$new(spdfs_list, "leafdown", input)
update_leafdown <- reactiveVal(0)
observeEvent(input$drill_down, {
my_leafdown$drill_down()
update_leafdown(update_leafdown() + 1)
})
observeEvent(input$drill_up, {
my_leafdown$drill_up()
update_leafdown(update_leafdown() + 1)
})
output$leafdown <- renderLeaflet({
update_leafdown()
meta_data <- my_leafdown$curr_data
curr_map_level <- my_leafdown$curr_map_level
if (curr_map_level == 1) {
data <- meta_data %>%
left_join(gdp_2014_federal_states, by = c("NAME_1" = "Federal_State"))
} else {
data <- meta_data %>%
left_join(gdp_2014_admin_districts, by = c("NAME_2" = "Admin_District"))
}
my_leafdown$add_data(data)
my_leafdown$draw_leafdown(
fillColor = ~ colorNumeric("Greens", GDP_2014)(GDP_2014), weight = 2, color = "grey"
)
})
}
shinyApp(ui, server)
}