Setup

The following two lines move some software to your computer. You should run the following chunk by itself then Make the two lines comments after you run them once.

# install.packages("leaflet")
# install.packages("tidyverse")

The following two lines make the packages available to your R session. Leave them here.

library(leaflet)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.1.2     ✓ dplyr   1.0.6
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

Locate my House

# The following two lines locate my house.
myLong = -122.8628
myLat = 47.018

First Leaflet Command

Use leaflet to put my house in context.

leaflet() %>% 
  addTiles() %>% 
  addMarkers(myLong,myLat,popup = "My House")

Use the myLocs file

myLocs <- read_csv("myLocs.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   lat = col_double(),
##   lon = col_double(),
##   ID = col_character()
## )
myMap = myLocs %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers(popup = ~ID)
## Assuming "lon" and "lat" are longitude and latitude, respectively
myMap

Providers

Look at the list of potential alternatives to the default OSM map.

Visit https://github.com/rstudio/leaflet.providers

I’ll pick a few options to demonstrate usage.

Stamen

myMap = myLocs %>% 
  leaflet() %>% 
  addProviderTiles("Stamen") %>% 
  addMarkers(popup = ~ID)
## Assuming "lon" and "lat" are longitude and latitude, respectively
myMap

CartoDB

myMap = myLocs %>% 
  leaflet() %>% 
  addProviderTiles("CartoDB") %>% 
  addMarkers(popup = ~ID)
## Assuming "lon" and "lat" are longitude and latitude, respectively
myMap

Esri

myMap = myLocs %>% 
  leaflet() %>% 
  addProviderTiles("Esri") %>% 
  addMarkers(popup = ~ID)
## Assuming "lon" and "lat" are longitude and latitude, respectively
myMap

`