Reputation: 425
I've built the following app https://iseak.shinyapps.io/Mapa/ and I would like to switch from tmap to leaflet for the map portion of the app because I've run into some limitations (I'd like fancier popups and some other stuff).
Currently the map is working with tmap using the following code:
output$map = renderLeaflet({
tm <- tm_shape(subsetData()) +
tm_fill(selvar(), palette=colores(),
alpha=0.95,
breaks=breaks(),
id="Zona",
popup.vars=c(" "="popover"),
popup.format=list(digits=2),
legend.show = F,
labels=c(" "=selvar())
) +
tm_borders() +
tmap_options(basemaps = "CartoDB.Positron"
)
tmap_leaflet(tm) %>%
removeLayersControl()
})
My starting point when trying to use leaflet directly is:
output$map = renderLeaflet({
leaflet(data=subsetData()) %>%
addProviderTiles("CartoDB.Positron") %>%
clearShapes() %>%
clearControls() %>%
addPolygons(data = subsetData(),
fillColor = ~colores()(selvar()),
fillOpacity = 0.9,
weight = 2)
})
But I keep getting errors. Please provide some pointers on how to easily switch from tmap to leaflet and how to use reactive values inside the leaflet call.
Upvotes: 0
Views: 303
Reputation: 96
This question is quite open. I think the leaflet website has quite good example code (https://rstudio.github.io/leaflet/). Browse through the left menu.
When it come to reactivity you don't need brackets to use variable created by reactiveValues
; in case of reactive()
you need some.
Looking at your code I'm not sure what is behind color() and selvar(). Assuming selvar() is the number for the color scale my approach would have been something like this:
selvar <- reactiveValues(n=c(1:10)
output$map = renderLeaflet({
pal <- leaflet::colorBin(brewer.pal(9,"Blues"), domain = selvar, bins = 7, na.color = "gray")
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data=shp,
fillColor = ~pal(selvar),
fillOpacity = 0.9,
weight = 2)
You can specify your shape data in leaflet()
or in addPolygons()
. Using the latter allows to use different shape file tables. Finally, another useful pointer is leafletProxy('map')
which would work well with the year buttons underneath the map for fast updates.
Upvotes: 0