Load the R Packages

suppressMessages(suppressWarnings({
library(xml2)
library(rvest)
library(curl)
      library(dplyr); library(plyr)
      library(leaflet)
}))

Extracting Latitude and Longitude Of World Capital Cities

The latitude and longitude of the world capital cities can be found here: http://www.lab.lmnixon.org/4th/worldcapitals.html
The data frame is extracted from the URL above using xml2, rvest and curl package.

web_content <- read_html(curl('http://www.lab.lmnixon.org/4th/worldcapitals.html', handle = new_handle("useragent" = "Mozilla/5.0")))
df1 <- (web_content %>% html_table(header=T))[[1]][1:200,]
head(df1)
         Country          Capital Latitude Longitude
1    Afghanistan            Kabul   34.28N    69.11E
2        Albania           Tirane   41.18N    19.49E
3        Algeria          Algiers   36.42N    03.08E
4 American Samoa        Pago Pago   14.16S   170.43W
5        Andorra Andorra la Vella   42.31N    01.32E
6         Angola           Luanda   08.50S    13.15E

Data Processing

The longitude and latitude of the data will be converted to numeric variables, where positive values refer to N(North) or E(East); negative values refer to S(South) or W(West).

df2 <- df1 %>% mutate(poslat = grepl(pattern = "N", Latitude), poslong = grepl(pattern = "E", Longitude))
df3 <- df2 %>% mutate(lat = as.numeric(gsub("N|S", "", Latitude)), lng = as.numeric(gsub("E|W", "", Longitude)))
df3 <- as.data.frame(df3)
u1 <- df3[,"poslat"]; u2 <- df3[,"poslong"]
df3[!u1, "lat"] <- df3[!u1, "lat"]*-1; df3[!u2, "lng"] <- df3[!u2, "lng"]*-1

#preview
head(df3)
##          Country          Capital Latitude Longitude poslat poslong    lat
## 1    Afghanistan            Kabul   34.28N    69.11E   TRUE    TRUE  34.28
## 2        Albania           Tirane   41.18N    19.49E   TRUE    TRUE  41.18
## 3        Algeria          Algiers   36.42N    03.08E   TRUE    TRUE  36.42
## 4 American Samoa        Pago Pago   14.16S   170.43W  FALSE   FALSE -14.16
## 5        Andorra Andorra la Vella   42.31N    01.32E   TRUE    TRUE  42.31
## 6         Angola           Luanda   08.50S    13.15E  FALSE    TRUE  -8.50
##       lng
## 1   69.11
## 2   19.49
## 3    3.08
## 4 -170.43
## 5    1.32
## 6   13.15

The Map!

Finally, we use the leaflet package to create the interactive map with the addition of markers (i.e. a “capital” icon) and popup (i.e. the name of the country and its capital).
Adding the parameter, clusterOptions = markerClusterOptions(), we are able to cluster the markers and show the number of markers (capitals) in a certain region.

capitalLabel <- paste0(df3[,"Country"], " - ", df3[,"Capital"])
myicon <- makeIcon( 
      iconUrl = "https://cdn2.iconfinder.com/data/icons/the-politics/350/Government-512.png",
      iconWidth = 31*215/230, iconHeight = 31,
      iconAnchorX = 31*215/230/2, iconAnchorY = 16
      
)

df3 %>% select(lat, lng) %>% leaflet() %>% addTiles() %>% addMarkers(icon = myicon, popup = capitalLabel, clusterOptions = markerClusterOptions())