getTables <-function(pageNumber) {# wait 2 seconds between each scrapeSys.sleep(2) url <-paste0("https://centercityphila.org/explore-center-city/ccd-sips/sips-list-view?page=", pageNumber)# read the page html html <-read_html(url)# extract table info table <- html |>html_node("table") |>html_table()# extract hyperlinks to specific restaurant/bar specials links <- html |>html_elements(".o-table__tag.ccd-text-link") |>html_attr("href") |>as_tibble()# add full hyperlinks to the table info tableSpecials <<-bind_cols(table, links) |>mutate(Specials =paste0(url, value)) |>select(-c(`CCD SIPS Specials`, value))}
# get remaining tablestable2 <-map_df(2:4, getTables) # combine all tablestable <-bind_rows(table1Mod, table2)table |>head(3)
# save table with scraped addresses to filewrite_rds(table,file =here(data, "specialsScraped.Rds"))
1. Scraping the data Scrape restaurants and addresses from the website with rvest
2. Geocoding the addresses Geocode the restaurant addresses to obtain geographical coordinates with tidygeocoder
3. Building the map Build an interactive map with leaflet
Geocoding
We will use the tidygeocoder package to help us convert the addresses to geographical coordinates, and we will specify we want to use the ArcGIS service
# Geocoding the addressesspecials <- table |>geocode(address = Address,method ="arcgis", long ="Longitude",lat ="Latitude")specials |>head(3)
leaflet(data = specials, options =tileOptions(minZoom =15,maxZoom =19)) |># add map markersaddCircles(lat =~ specials$Latitude, lng =~ specials$Longitude, popup = specials$Address,label =~ Name,# customize labelslabelOptions =labelOptions(style =list("font-family"="Red Hat Text, sans-serif","font-size"="1.2em") ) ) |># add map tiles in the backgroundaddProviderTiles(providers$CartoDB.Positron) |># set the map viewsetView(mean(specials$Longitude), mean(specials$Latitude), zoom =16)
Adding fullscreen control
leaflet(data = specials,options =tileOptions(minZoom =15,maxZoom =19)) |># add map markersaddCircles(lat =~ specials$Latitude, lng =~ specials$Longitude, popup = specials$Address,label =~ Name,# customize labelslabelOptions =labelOptions(style =list("font-family"="Red Hat Text, sans-serif","font-size"="1.2em") ) ) |># add map tiles in the backgroundaddProviderTiles(providers$CartoDB.Positron) |># set the map viewsetView(mean(specials$Longitude), mean(specials$Latitude), zoom =16) |># add fullscreen control button leaflet.extras::addFullscreenControl()
Customizing map markers: styling
# marker for the restaurants/barspopInfoCircles <-paste("<h2 style='font-family: Red Hat Text, sans-serif; font-size: 1.6em; color:#43464C;'>", "<a style='color: #00857A;' href=", specials$Specials, ">", specials$Name, "</a></h2>","<p style='font-family: Red Hat Text, sans-serif; font-weight: normal; font-size: 1.5em; color:#9197A6;'>", specials$Address, "</p>" )
In formatted HTML
<!--heading: restaurant/bar name--><h2style="font-family: Red Hat Text, sans-serif; font-size: 1.6em; color:#43464C;"><!--hyperlinks: link to the special--><astyle="color: #00857A;" href="https://centercityphila.org/explore-center-city/ccd-sips/sips-list-view?page=1#1028-yamitsuki-sushi-ramen"> 1028 Yamitsuki Sushi & Ramen </a></h2><!--paragraph: address--><pstyle="font-family: Red Hat Text, sans-serif; font-weight: normal; font-size: 1.5em; color:#9197A6;">1028 Arch Street, Philadelphia, PA 19107 </p>
Customizing map markers: map
leaflet(data = specials, options =tileOptions(minZoom =15,maxZoom =19)) |># add map markersaddCircles(lat =~ specials$Latitude, lng =~ specials$Longitude,# customize markersfillColor ="#009E91",fillOpacity =0.6, stroke = F,radius =12,# customize pop-upspopup = popInfoCircles,label =~ Name,# customize labelslabelOptions =labelOptions(style =list("font-family"="Red Hat Text, sans-serif","font-size"="1.2em") ) ) |># add map tiles in the backgroundaddProviderTiles(providers$CartoDB.Positron) |># set the map viewsetView(mean(specials$Longitude), mean(specials$Latitude), zoom =16) |># add fullscreen control button leaflet.extras::addFullscreenControl()
Adding a marker at the center: styling
# marker for the center of the mappopInfoMarker <-paste("<h1 style='padding-top: 0.5em; margin-top: 1em; margin-bottom: 0.5em; font-family: Red Hat Text, sans-serif; font-size: 1.8em; color:#43464C;'>", "<a style='color: #00857A;' href='https://centercityphila.org/explore-center-city/ccdsips'>","Center City District Sips 2022", "</a>","</h1>","<p style='color:#9197A6; font-family: Red Hat Text, sans-serif; font-size: 1.5em; padding-bottom: 1em;'>", "Philadelphia, PA", "</p>" )
# custom icon for the center of the mapcenterIcon <-makeAwesomeIcon(icon ="map-pin",iconColor ="#FFFFFF",markerColor ="darkblue", # accepts HTML colorslibrary ="fa" )
Adding a marker at the center: map
leaflet(data = specials, options =tileOptions(minZoom =15,maxZoom =19)) |># add map markersaddCircles(lat =~ specials$Latitude, lng =~ specials$Longitude, # customize markersfillColor ="#009E91",fillOpacity =0.6, stroke = F,radius =12,# customize pop-upspopup = popInfoCircles,label =~ Name,# customize labelslabelOptions =labelOptions(style =list("font-family"="Red Hat Text, sans-serif","font-size"="1.2em") ) ) |># add map tiles in the backgroundaddProviderTiles(providers$CartoDB.Positron) |># set the map viewsetView(mean(specials$Longitude), mean(specials$Latitude), zoom =16) |># add fullscreen control button leaflet.extras::addFullscreenControl() |># add marker at the centeraddAwesomeMarkers(icon = centerIcon,lng =mean(specials$Longitude), lat =mean(specials$Latitude), label ="Center City District Sips 2022",labelOptions =labelOptions(style =list("font-family"="Red Hat Text, sans-serif","font-size"="1.2em") ),popup = popInfoMarker,popupOptions =popupOptions(maxWidth =250))