Leafdown: Leafdown R6 Class

Description Active bindings Methods Examples

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...). At the moment only two map levels are possible.

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 = c(GID_1 = "GID_1")
)
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 with the columns by which the map levels should be joined.


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

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

leafdown documentation built on April 29, 2021, 9:06 a.m.