3

When I add a simple map to my shiny application, without adding polygons, the zoom argument works correctly, and the map initializes as it should.

On the other hand, when I add polygons with add_polygon(), the map is initialized more zoomed out than it should.

Why is this happening?

This is my code:

library(mapdeck)
library(sf)

sf = st_as_sf(my_LargeSpatialPolygonDataframe)

output$my_map = renderMapdeck({

    mapdeck(token = mytoken, location = c(a, b), zoom = 12,
            bearing = -45.00, pitch = 0, style = 'mapbox://styles/mapbox/light-v9') %>%

    add_polygon(data = sf,
                stroke_colour = "#000000",
                stroke_width = 20,
                stroke_opacity = 200,
                fill_opacity = 0,
                layer = "init_polygons")
})

somberlain
  • 69
  • 6
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and the packages you're using (`sf`?) – camille May 16 '19 at 15:08

1 Answers1

4

When you add a layer through one of the add_ functions, the layer will calculate the zoom level required to fit the whole of the data in the frame of the window. You can stop this behaviour using update_view = FALSE

Here's a reproducible example

setting location in mapdeck()

Here the add_polygon() layer will re-zoom the map to show all the data in the layer. This is the behaviour you're seeing.

library(mapdeck)

set_token("MAPBOX_TOKEN")

mapdeck(
  location = c(144.9, -37.8)
  , zoom = 11
  , bearing = -45.00
  , pitch = 0
  , style = mapdeck_style("light")
  ) %>%
  add_polygon(
    data = spatialwidget::widget_melbourne
    , stroke_colour = "#000000"
    , stroke_width = 20
    , stroke_opacity = 200
    , fill_opacity = 0
    , layer_id = "init_polygons"
    )

enter image description here

using update_view = FALSE

This tells the layer to not update the view, so your original location() values will be used.

mapdeck(
  location = c(144.9, -37.8)
  , zoom = 11
  , bearing = -45.00
  , pitch = 0
  , style = mapdeck_style("light")
  ) %>%
  add_polygon(
    data = spatialwidget::widget_melbourne
    , stroke_colour = "#000000"
    , stroke_width = 20
    , stroke_opacity = 200
    , fill_opacity = 0
    , layer_id = "init_polygons"
    , update_view = F
    )

enter image description here

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139