Singapore Hotels: An Interactive Map

As part of the “Developing Data Products” course on Coursera, part of the Data Science Specialization offered by Johns Hopkins University, I have been tasked to build an interactive map.

As the course gives me free reign to build whatever I want, I shall be building a map of hotels in and around Singapore.

Methodology

To generate data for this, I utilized the Google Places API to get data for the query “hotels in singapore”. The resulting JSON was processed in a python script to generate a clean and compatible csv file. The data can be downloaded from the Appendix below.

Processing

The processing of the CSV file consisted of the following steps:

  1. Load the csv file using readr::read_csv
  2. Add a reasonable popup message column to the dataframe
  3. Process the dataframe using the leaflet library to generate a clustered map of the hotels.
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(leaflet))

# Define Column Types
col_spec = cols(
  name = col_character(),
  address = col_character(),
  phone = col_character(),
  rating = col_double(),
  url = col_character(),
  website = col_character(),
  lat = col_double(),
  lng = col_double()
)


# Convenience function to generate popups
generate_popup <- function(name, address, phone, rating, mapurl, website) {
  popup <- paste(sep = "<br/>",
                 paste0("<b><a href=", website, ">", name, "</a></b>"),"", address, phone, paste0("Rating: ", rating),"", paste0("<b><a href=", mapurl, ">Google Map Link</a></b>"))
  popup
}

# Read and modify the data
hotels <- read_csv("singhotels.csv", col_types = col_spec)
hotels <- hotels %>% 
  mutate(popup = generate_popup(name, address, phone, rating, url, website))

Result

Following is the resulting Map

hotels %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers(~lng, ~lat, label=~name, popup=~popup, clusterOptions = markerClusterOptions())

Appendix

You can download the original processed data file by clicking the link: (Get data)