Leaflet Demo

Harold Nelson

2023-02-06

Problem

Given a CSV file with the the name of a state, the latitude and longitude of its centroid, write an R program using the leaflet package to produce a map with popups at each centroid showing the name of the state.

Solution

library(leaflet)

# Load data from CSV file
states <- read.csv("State Centroids.csv")

# Create a leaflet map
leaflet(states) %>% 
  addTiles() %>% 
  addMarkers(lng = states$Longitude, 
             lat = states$Latitude, 
             popup = states$State)