Leafdown {leafdown}R Documentation

Leafdown R6 Class

Description

This class acts as a wrapper around a leafdown map.

Active bindings

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.

Methods

Public methods


Method new()

Initializes the leafdown object.

Usage
Leafdown$new(spdfs_list, map_output_id, input, join_map_levels_by = NULL)
Arguments
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.


Method draw_leafdown()

Draws the leaflet map on the current map level. All unselected parents will be drawn in gray.

Usage
Leafdown$draw_leafdown(...)
Arguments
...

Additional arguments given to leaflet::addPolygons


Method keep_zoom()

Keeps the zoom after drill_down and drill_up events.

Usage
Leafdown$keep_zoom(map, input)
Arguments
map

the map output from draw_leafdown

input

the input object from the shiny app


Method add_data()

Adds the data to the currently displayed shapes. This includes the meta-data AND the values to be visualized in the map.

Usage
Leafdown$add_data(data)
Arguments
data

The new data existing of the meta-data and the values to display in the map(color)


Method drill_down()

Drills down to the lower level if:

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.

Usage
Leafdown$drill_down()

Method drill_up()

Drills up to the higher level if:

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.

Usage
Leafdown$drill_up()

Method toggle_shape_select()

Selects the shape with the given shape id, or unselects it if it was already selected.

Usage
Leafdown$toggle_shape_select(shape_id)
Arguments
shape_id

the id of the shape to select, has to be a character and in the current map-level.


Method clone()

The objects of this class are cloneable with this method.

Usage
Leafdown$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

## 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)


[Package leafdown version 1.2.0 Index]