| Title: | Provides Drill Down Functionality for 'leaflet' Choropleths |
|---|---|
| Description: | Provides drill down functionality for 'leaflet' choropleths in 'shiny' apps. |
| Authors: | Andreas Hofheinz [cre], Peter Gandenberger [aut] |
| Maintainer: | Andreas Hofheinz <[email protected]> |
| License: | GPL-3 |
| Version: | 1.2.0 |
| Built: | 2026-05-21 09:26:18 UTC |
| Source: | https://github.com/cran/leafdown |
The join_map_levels_by must be a named vector of at most one element. The columns specified in the vector must be data slots of the spdfs in the spdfs_list.
assert_join_map_levels_by(join_map_levels_by, spdfs_list)assert_join_map_levels_by(join_map_levels_by, spdfs_list)
join_map_levels_by |
A named vector with the columns to join the map levels by. |
spdfs_list |
A list with the spdfs of all map levels. |
the join_map_levels_by in the right order
The spdf_list must be a list of at most two elements. All elements must be a s4 class of type SpatialPolygonsDataFrame.
assert_spdf_list(spdfs_list)assert_spdf_list(spdfs_list)
spdfs_list |
A list with the spdfs of all map levels |
TRUE if spdf_list is valid.
Checks arguments in ellipsis for undesired inputs such as 'layerId' which may collide with internal structure of leafdown and returns a "cleaned" version of the arguments by removing or redefining problematic inputs. e.g. 'layerId' is removed from arg_list when set.
check_draw_ellipsis(...)check_draw_ellipsis(...)
... |
Additional arguments given to |
List containing arguments in ... as elements
A dataset containing the GPD (gross domestic product) for 402 administrative districts of Germany for the year 2014.
gdp_2014_admin_districtsgdp_2014_admin_districts
A data frame with 402 rows and 2 variables:
Name of the administrative district
GDP for the year 2014, in euro
Landatlas (www.landatlas.de). Ausgabe 2018. Hrsg.: Thuenen-Institut fuer Laendliche Raeume - Braunschweig 2018.
Note that in this package we have slightly adapted some names of the administrative districts for a better match.
A dataset containing the GPD (gross domestic product) for all 16 federal states of Germany for the year 2014.
gdp_2014_federal_statesgdp_2014_federal_states
A data frame with 16 rows and 2 variables:
Name of the federal state
GDP for the year 2014, in euro
Arbeitskreis Volkswirtschaftliche Gesamtrechnungen der Laender: https://www.deutschlandinzahlen.de
This class acts as a wrapper around a leafdown map.
curr_sel_dataA reactiveValue containing a data.frame with
the metadata and (if available) the corresponding values of all currently selected shapes.
curr_dataThe metadata and (if available) the corresponding values of all currently displayed shapes.
curr_map_levelIndex 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_idsThe 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_listA list with the spdfs of all map levels. This cannot be changed later.
map_output_idThe id from the shiny-ui used in the leafletOutput("<<id>>").
Used to observe for _shape_click events.
inputThe input from the shiny app.
join_map_levels_byA 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.
Leafdown$draw_leafdown(...)
...Additional arguments given to leaflet::addPolygons
keep_zoom()
Keeps the zoom after drill_down and drill_up events.
Leafdown$keep_zoom(map, input)
mapthe map output from draw_leafdown
inputthe input object from the shiny app
add_data()
Adds the data to the currently displayed shapes. This includes the meta-data AND the values to be visualized in the map.
Leafdown$add_data(data)
dataThe new data existing of the meta-data and the values to display in the map(color)
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.
Leafdown$drill_down()
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.
Leafdown$drill_up()
toggle_shape_select()
Selects the shape with the given shape id, or unselects it if it was already selected.
Leafdown$toggle_shape_select(shape_id)
shape_idthe id of the shape to select, has to be a character and in the current map-level.
clone()
The objects of this class are cloneable with this method.
Leafdown$clone(deep = FALSE)
deepWhether to make a deep clone.
## Not run: 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) ## End(Not run)## Not run: 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) ## End(Not run)
A dataset containing the results of the presidential election and census data (e.g. racial makeup, unemployment)
us_election_countiesus_election_counties
A data frame with 3,143 rows and 17 total columns
Name of the State
Abbreviation of the State name
Name of the County
Total number of votes cast
Percent of votes for the Republican Party
Percent of votes for the Democratic Party
Percent of votes for the Green Party
Percent of votes for the Libertarian Party
Total Population of the county
Percent of unemployment
Percentage of Whites
Percentage of Blacks
Percentage of Hispanics
Percentage of Asians
Percentage of Amerindians
Percentage of Other Races
The short County name, used for matching with the map
https://github.com/Deleetdk/USA.county.data
A dataset containing the results of the presidential election and census data (e.g. racial makeup, unemployment)
us_election_statesus_election_states
A data frame with 51 rows and 15 total columns
Name of the State
Abbreviation of the State name
Total number of votes cast
Percent of votes for the Republican Party
Percent of votes for the Democratic Party
Percent of votes for the Green Party
Percent of votes for the Libertarian Party
Total Population of the county
Percent of unemployment
Percentage of Whites
Percentage of Blacks
Percentage of Hispanics
Percentage of Asians
Percentage of Amerindians
Percentage of Other Races
https://github.com/Deleetdk/USA.county.data
Note: The data was aggregated from the county level