library(mapdeck)

Layers

All layers support Simple Feature (from library(sf)) objects.

layer_id

Each layer requires a layer_id value. If you don’t supply one, it will create it for you, but this may have some unintended consequences for you.

The layer_id is the ID that deck.gl uses to ‘shallow-compare’ layers to see if they need updating. Which comes in handy when working in Shiny. If you update data, for example when using a reactive() function, and use the same layer_id, deck.gl knows it should update the layer rather than re-draw it completely.

However, if two of your layers have the same layer_id, then deck.gl will get confused and start updating the wrong thing. So while I’ve made the layer_id argument optional, you should still set it manually when using it in Shiny apps.

Arcs

Arcs

Arcs

sf : supports sf objects with two POINT or MULTIPOINT geometry columns.

GeoJSON

GeoJSON

GeoJSON

sf : supports all sf geometry types. Converted to geojson using geojsonsf::sf_geojson(). Useful if your sf object contains different geometry types.

Great Circles

Great Circles

Great Circles

sf : supports sf objects with two POINT or MULTIPOINT geometry columns.

Lines

Lines

Lines

sf : supports sf objects with two POINT and MULTIPOINT geometry columns.

Path

Path

Path

sf : supports sf objects with LINESTRING and MULTILINESTRING geometries

Point cloud

Pointcloud

Pointcloud

sf : supports sf objects with POINT and MULTIPOINT geometries

Polygons

Polygons

Polygons

sf : supports sf objects with POLYGON and MULTIPOLYGON geometries

Text

Text

Text

sf : supports sf objects with POINT and MULTIPIONT geometries

Shiny

The three main functions to use are

  1. renderMapdeck() in the Server
  2. mapdeckOutput() in the UI
  3. mapdeck_update() to update an existing map

You can observe interactions with the layers by using observeEvent(). The event you observe is formed by combining

'map_id + "_" + layer + "_click"

where

  • map_id is the outputId of the mapdeckOutput() function
  • layer is the layer you’re interacting with
  • _click is you observing ‘clicking’ on the layer

There’s an example of this in the shiny.

library(shiny)
library(shinydashboard)
library(jsonify)

ui <- dashboardPage(
    dashboardHeader()
    , dashboardSidebar()
    , dashboardBody(
        mapdeckOutput(
            outputId = 'myMap'
            ),
        sliderInput(
            inputId = "longitudes"
            , label = "Longitudes"
            , min = -180
            , max = 180
            , value = c(-180,-90)
        )
        , verbatimTextOutput(
            outputId = "observed_click"
        )
    )
)
server <- function(input, output) {
    
    #set_token('abc') ## set your access token
    
    origin <- capitals[capitals$country == "Australia", ]
    destination <- capitals[capitals$country != "Australia", ]
    origin$key <- 1L
    destination$key <- 1L
    
    df <- merge(origin, destination, by = 'key', all = T)
    
    output$myMap <- renderMapdeck({
        mapdeck(style = mapdeck_style('dark')) 
    })
    
    ## plot points & lines according to the selected longitudes
    df_reactive <- reactive({
        if(is.null(input$longitudes)) return(NULL)
        lons <- input$longitudes
        return(
            df[df$lon.y >= lons[1] & df$lon.y <= lons[2], ]
        )
    })
    
    observeEvent({input$longitudes}, {
        if(is.null(input$longitudes)) return()
        
        mapdeck_update(map_id = 'myMap') %>%
            add_scatterplot(
                data = df_reactive()
                , lon = "lon.y"
                , lat = "lat.y"
                , fill_colour = "country.y"
                , radius = 100000
                , layer_id = "myScatterLayer"
                , update_view = FALSE
            ) %>%
            add_arc(
                data = df_reactive()
                , origin = c("lon.x", "lat.x")
                , destination = c("lon.y", "lat.y")
                , stroke_from = "country.x"
                , stroke_to = "country.y"
                , layer_id = "myArcLayer"
                , id = "country.x"
                , stroke_width = 4
                , update_view = FALSE
            )
    })
    
    ## observe clicking on a line and return the text
    observeEvent(input$myMap_arc_click, {
        
        event <- input$myMap_arc_click
        output$observed_click <- renderText({
            jsonify::pretty_json( event )
        })
    })
}

shinyApp(ui, server)
Shiny

Shiny