Using leaflet in R is fairly simple and there are plenty of online resources and examples available already. See the RStudio tutorial to get started if you haven’t already. This post presents a series of examples which build upon each other.

The code displays both point data and raster data on leaflet maps with Shiny app integration. The focus here is to show the utility of event observation on the server side for integrating the behavior of browser elements, namely, the leaflet map.

In the course of these examples, I’ll also use a modal from the shinyBS package and the maps will be full size in the browser window with UI widgets laid on top using absolute panels. The full post contains interactive embedded Shiny widgets. See the full post here . It includes complete source code for these and other related apps using leaflet.

The app does not do much in this version, but it provides a clear view of how to use observers in Shiny apps to ensure that various inputs and outputs remain mutually well behaved. Here I use observeEvent three distinct times to tie together the reactive behavior of instances of selectInput and leafletOutput . The entire ui.R at this point is:

In server.R note the three observers. They constitute the entire code block except for the initial, relatively simple call to renderLeaflet . Comments mark the role performed by each call to observeEvent , which:

Care must be taken to ensure each observer does not update when it shouldn’t. One can easily trigger another back and forth in this context. Fortunately it is not too difficult to make the three observeEvent calls work well together. See the full post to use the app and continue with the interactive examples.