rm(list=ls(all=T))
gc()
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 496098 26.5     940480 50.3   940480 50.3
Vcells 935205  7.2    1650153 12.6  1208273  9.3
options(digits=4, scipen=40)
library(dplyr)
library(leaflet)

Read Data

ypath = "/home/tonychuo/_S2018/yelp10/prep2/"
load(paste0(ypath, "businesses.rdata"))
table(B$city) %>% sort %>% tail(8)

      Mesa   Montréal Pittsburgh Scottsdale  Charlotte    Toronto    Phoenix  Las Vegas 
      5129       5162       5680       7500       7542      15465      15599      24697 
c(range(B$latitude), range(B$longitude))
[1] NA NA NA NA

Set Location

B = subset(B, city == "Las Vegas" & !is.na(address) & state == "NV" & 
             B$latitude > 0 & B$longitude > -116)

Create popups in html format

url = "'http://cm.nsysu.edu.tw/~msrc/wp/'"
B$popup = paste(
  sep="<br/>",
  sprintf("<b><a href=%s>%s</a></b>",url,B$name),
  sprintf("Stars:%.1f, Reviews:%d",B$stars,B$review_count),
  # sprintf("lat:%.2f, lon:%.2f",B$latitude,B$longitude),
  B$address)

Patition the B data_frame by the no. review

B$review_count %>% cut(c(0,5,10,50,100,200,400,9999)) %>% table
.
      (0,5]      (5,10]     (10,50]    (50,100]   (100,200]   (200,400] (400,1e+04] 
       5821        4430        8253        2130        1273         823         673 
B$rank = cut(B$review_count, c(0,5,10,50,100,200,400,9999))
bx = split(B, B$rank)

Create the map object

l <- leaflet() %>% addTiles() 
# add a level of markers per partition
names(bx) %>% purrr::walk( function(df) {
  l <<- l %>% addMarkers(
    data=bx[[df]], lng=~longitude, lat=~latitude,
    label=~name, popup=~popup, group = df,
    clusterOptions=markerClusterOptions(removeOutsideVisibleBounds=F),
    labelOptions = labelOptions(noHide=F, direction='auto')) 
  })

Interactive Map

l %>% addLayersControl(
  overlayGroups = names(bx),
  options = layersControlOptions(collapsed = FALSE) )