Creating an interactive map on R Markdown using leaflet package

In this work we are going to plot an interactive map from the restaurants in Paris. The data was obtained in https://www.kaggle.com/svrcek/paris-restaurants.

library(leaflet)
## Warning: package 'leaflet' was built under R version 4.0.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.2
## ── Attaching packages ────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ stringr 1.4.0
## ✓ tidyr   1.1.0     ✓ forcats 0.5.0
## ✓ readr   1.3.1
## Warning: package 'ggplot2' was built under R version 4.0.2
## ── Conflicts ───────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(tidyr)
set.seed(1986)
rest.paris <- read.csv2("restaurants-casvp.csv")
head(rest.paris)
##    code Nom.restaurant                    adresse ville                  tt
## 1 75018   CLIGNANCOURT 14-16, SQUARE CLIGNANCOURT PARIS 48.893493, 2.347233
## 2 75014       ARBUSTES        9, RUE DES ARBUSTES PARIS 48.828431, 2.309284
## 3 75014       BEAUNIER           43, RUE BEAUNIER PARIS 48.824459, 2.327765
## 4 75011         CHANZY           6, RUE DE CHANZY PARIS 48.852411, 2.382525
## 5 75009        NAVARIN         12, RUE DE NAVARIN PARIS 48.879688, 2.338996
## 6 75006  ANDRE MALRAUX         112, RUE DE RENNES PARIS 48.847869, 2.327529
##   TYPE
## 1    E
## 2    E
## 3    E
## 4    S
## 5    E
## 6    E

From this data set, we will keep the columns with the restaurant’s name (Nom.restaurant) and the latitude and longitude coordinates (tt). We will also split the column tt into two, separating latitude from longitude coordinates.

rest.paris <- rest.paris %>% select("Nom.restaurant", "tt")
colnames(rest.paris) <- c("Restaurant", "tt")
rest.paris <- rest.paris[complete.cases(rest.paris),]
rest.paris <- separate(rest.paris, col = tt, into = c("Latitude","Longitude"), sep = ",")
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 1 rows [14].
rest.paris$Latitude <- as.numeric(rest.paris$Latitude)
rest.paris$Longitude <- as.numeric(rest.paris$Longitude)
head(rest.paris)
##      Restaurant Latitude Longitude
## 1  CLIGNANCOURT 48.89349  2.347233
## 2      ARBUSTES 48.82843  2.309284
## 3      BEAUNIER 48.82446  2.327765
## 4        CHANZY 48.85241  2.382525
## 5       NAVARIN 48.87969  2.338996
## 6 ANDRE MALRAUX 48.84787  2.327529

Now we are going to plot the interactive map using the data from the data frame above.

rest.map <- rest.paris %>% leaflet() %>% addTiles() %>% 
        setView(lat = 48.85, lng = 2.35, zoom = 12) %>%
        addMarkers(lat = rest.paris$Latitude, lng = rest.paris$Longitude, 
                   popup = rest.paris$Restaurant)
## Warning in validateCoords(lng, lat, funcName): Data contains 1 rows with either
## missing or invalid lat/lon values and will be ignored
rest.map