Creating a Interactive Leaflet Map with Shiny in R: A Beginner's Guide

Introduction to Leaflet Map with Shiny in R

=====================================================

In this article, we will explore how to create a Leaflet map using the Shiny framework in R. We will cover the basics of creating a Shiny app and use the Leaflet package to visualize data on an interactive map.

Prerequisites


Before starting, make sure you have the following packages installed:

  • shiny
  • leaflet

You can install them using the following commands:

install.packages("shiny")
install.packages("leaflet")

Creating a Shiny App with Leaflet Map


Let’s create a simple Shiny app that displays a Leaflet map. We will use the fluidPage function to define the UI and the renderLeaflet function to render the map.

# ui.R
library(shiny)
library(leaflet)

ui <- fluidPage(
  selectInput(inputId = "Selector",
              label = "Please Select Type of Person",
              choices = c("Please Select:",
                          "Type A",
                          "Type B"),
              selected = "Please Select:"),

  leafletOutput("myMap")
)

In this code, we define a selectInput widget to allow the user to select a type of person. We also create a leafletOutput widget to render the map.

Defining the Server Function


Now, let’s define the server function that will handle the rendering of the map.

# server.R
library(shiny)

server <- function(input, output) {
  output$myMap <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      addMarkers(data = SampleData,
                 ~Longitude,
                 ~Latitude)
  })

  observeEvent(input$Selector, {
    leafletProxy("myMap") %>% 
      clearGroup("myMarkers") %>% 
      addMarkers(data = SampleData[SampleData$type == input$Selector, ],
                 ~Longitude,
                 ~Latitude,
                 group = "myMarkers")
  })
}

In this code, we define a renderLeaflet function to render the map. We also use an observeEvent function to observe changes in the select input and update the markers on the map.

Creating Sample Data


Let’s create some sample data to demonstrate the app.

SampleData <- data.frame(type = c('Type A', 'Type B', 'Type A', 'Type B'),
                         Longitude = c(80.1, 80.2, 80.3, 80.4),
                         Latitude = c(40.1, 40.2, 40.3, 40.4))

Putting it All Together


Let’s put everything together to create a complete Shiny app.

# R script
library(shiny)
library(leaflet)

SampleData <- data.frame(type = c('Type A', 'Type B', 'Type A', 'Type B'),
                         Longitude = c(80.1, 80.2, 80.3, 80.4),
                         Latitude = c(40.1, 40.2, 40.3, 40.4))

ui <- fluidPage(
  selectInput(inputId = "Selector",
              label = "Please Select Type of Person",
              choices = c("Please Select:",
                          "Type A",
                          "Type B"),
              selected = "Please Select:"),

  leafletOutput("myMap")
)

server <- function(input, output) {
  output$myMap <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      addMarkers(data = SampleData,
                 ~Longitude,
                 ~Latitude)
  })

  observeEvent(input$Selector, {
    leafletProxy("myMap") %>% 
      clearGroup("myMarkers") %>% 
      addMarkers(data = SampleData[SampleData$type == input$Selector, ],
                 ~Longitude,
                 ~Latitude,
                 group = "myMarkers")
  })
}

shinyApp(ui, server)

Conclusion


In this article, we demonstrated how to create a Leaflet map using the Shiny framework in R. We covered the basics of creating a Shiny app and used the Leaflet package to visualize data on an interactive map.

Troubleshooting


  • Make sure you have the required packages installed (shiny and leaflet)
  • Check that your sample data is correct and can be accessed from the server
  • Verify that the UI and server functions are correctly connected

Next Steps


  • Explore other Leaflet features, such as markers with pop-ups or overlays
  • Add more complex data sources to the map, such as GeoJSON files
  • Use Shiny’s built-in tools for debugging and testing

Last modified on 2024-01-29