4 tricks for working with R, Leaflet and Shiny

I recently worked on a dataviz project involving Shiny and the Leaflet library. In this post I give 4 handy tricks we used to improve the app: 1/ how to use leaflet native widgets 2/ how to trigger an action when user clicks on map 3/ how to add a research bar on your map 4/ how to propose a “geolocalize me” button. For each trick, a reproducible code snippet is provided, so you just have to copy and paste it to reproduce the image.

Trick 1: use leaflet native control widget

Shiny is used to add interactivity to your dataviz. Working on maps, it’s great to add a widget to allow users to switch between datasets, using one layer or another… Of course, this can be achieved using a regular RadioButton or any other shiny widget, building a new map each time.

But Leaflet enables you to build the control widget directly while doing the map. Every layer you create must be added to the same map, all attributed to a “group“. Then, the control widget will allow you to switch from one group to another. The code below should help you understand the concept, and everything is explained in more detail on the amazing Rstudio tutorial.

Trick 2: make a graph depending on click position

It is possible to code the map so that clicking on a certain point opens particular information. In this example, the user chooses a marker on the map, which makes either a barplot OR a scatterplot. By adding an ID to every marker of the map, using the layerId argument, the click information is saved in a reactive value than can be used to run specific functions!

Trick 3: add a search bar

The ggmap library allows you to get the coordinates of any place in the world based on a google search. You can use this feature to create a search bar in addition to the leaflet map! The textInput is passed to the geocode function of ggmap. It returns coordinates that are given to leaflet to zoom on the map.

Trick 4: use geolocalization

When the user clicks on the “localize me” button, the leaflet map automatically zooms to the user’s position. This trick uses a Javascript function (but no Javascript knowledge needed). This example is inspired from here.


This post has been first published on the R graph gallery, a website that displays hundreds of R charts, always with the reproducible code! You can follow the gallery on Twitter: @R_Graph_Gallery

Code | Trick1 | Widget


Code | Trick2 | Graph depends on click

Code | Trick3 | Search Bar

Code | Trick4 | Geolocalization

3 thoughts on “4 tricks for working with R, Leaflet and Shiny

  1. Hi,
    very nice tools! Thank you for this page!
    Is it possible to do this with polygons? To select data by clicking at different polygons on a leaflet map?

    Thank you for your answer


    1. Hi Sven. It is indeed possible. You have to use input$map_shape_click instead of input$map_marker_click. Can you please try this code and tell me if it suits your needs? I will add this example to the gallery soon:

      adm < - getData('GADM', country='UKR', level=1) server <- function(input, output) { # create a reactive value that will store the click position data_of_click <- reactiveValues(clickedMarker=NULL) # Leaflet map with 2 markers output$map <- renderLeaflet({ leaflet() %>%
      addTiles() %>%
      addPolygons(data=adm, weight = 2, fillColor = “yellow”, layerId=~ID_1, popup=~NAME_1)

      # store the click
      data_of_click$clickedMarker <- input$map_shape_click }) # Make a barplot or scatterplot depending of the selected point output$plot=renderPlot({ my_place=data_of_click$clickedMarker$id print(my_place) if(is.null(my_place)){my_place=1} if(as.numeric(my_place)<15){ plot(rnorm(1000), col=rgb(0.9,0.4,0.1,0.3), cex=3, pch=20) }else{ barplot(rnorm(10), col=rgb(0.1,0.4,0.9,0.3)) } }) } ui <- fluidPage( br(), column(8,leafletOutput("map", height="600px")), column(4,br(),br(),br(),br(),plotOutput("plot", height="300px")), br() ) shinyApp(ui = ui, server = server)

Leave a Reply

Your email address will not be published.