Chapter 8 Interactive Maps with Shiny

8.1 Learning Objectives

  • Create a basic Shiny app using leaflet

8.2 Topics Learned

  • Shiny (UI vs. server)
  • Interactivity

Find the Shiny cheatsheet in RStudio under Help > Cheatsheets > Web Applications with shiny

8.3 Overview

This workshop teaches how to create a basic Shiny web app using leaflet.

8.4 Interactive Tutorial

This workshop’s Shiny app code can be found here.


  1. Create a new Shiny app from a template in R. Run the app. Which parts of the Shiny UI code map to the app? How are ui and server linked (what are the features that are the same across both?)

  2. Change the title of the app.


  1. In the UI object, add a leafletOutput("map") call in the mainPanel() function. Then, in the server object, add a output$map <- renderLeaflet({}) call.

That is, fill in the following script:

ui <- fluidPage(
   # Application title
   titlePanel("World Population Over Time"),
   # Sidebar with a slider input for number of bins 
         # sliderInput("bins",
         #             "Number of bins:",
         #             min = 1,
         #             max = 50,
         #             value = 30),

      # Specifies what to put in the main panel
         # Put one line of code here

server <- function(input, output) {
   # output$distPlot <- renderPlot({
   #    # generate bins based on input$bins from ui.R
   #    x    <- faithful[, 2] 
   #    bins <- seq(min(x), max(x), length.out = input$bins + 1)
   #    # draw the histogram with the specified number of bins
   #    hist(x, breaks = bins, col = 'darkgray', border = 'white')
   # })

    output$map <- renderLeaflet({
      # Put three lines of leaflet code here



  1. In the UI object, add a sliderInput of "year". Change the step size to 5, and remove the comma for thousands (hint: do ?sliderInput to look at the documentation, and options).


  1. Create a new variable called pop_per_year that is a subset of city by year, depending on which year you enter (input$year). Use the filter() command in the dplyr package.


  1. Try resizing the marker size depending on population, adding a popup, or doing more to customize your map!

  2. Try adding a feature in your app so that you only show cities over a certain population in millions (specified by the user), using numericInput() instead of sliderInput().


  1. Add a data table element with renderDataTable() and dataTableOutput() so you can see the attributes of the points in the map.

You can share your Shiny apps publicly by creating an account at and clicking Publish in the top of your app script.

8.5 R Training Workshop

I will be teaching a day-long “R for Social Scientists” Data Carpentry workshop on April 12 at the Center for Spatial Data Science.

Topics to be covered include:

  • Introduction to R
  • Working with data types, strings, and dates in R
  • Manipulating data frames in R
  • Data visualization in R

…and lunch will be provided!

Please register at this link if you are interested!