Last active
August 17, 2019 01:33
-
-
Save yonicd/04c5549e4b489efe003a51099de0b790 to your computer and use it in GitHub Desktop.
harvesting cvs store locations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(xml2) | |
library(rvest) | |
library(glue) | |
library(dplyr) | |
library(purrr) | |
library(purrrogress) | |
library(ggmap) | |
library(ggplot2) | |
srm <- function(x,pat = '\\t+|\\n+',replace = '') gsub(pat,replace,x) | |
get_cities <- function(x){ | |
x%>% | |
xml2::read_html()%>% | |
rvest::xml_nodes(xpath='//div[contains(@class, "states")]')%>% | |
rvest::html_text()%>% | |
srm()%>% | |
srm('\\)',',')%>% | |
srm('\\s\\([1-9]','')%>% | |
strsplit(', ')%>% | |
purrr::flatten_chr()%>% | |
srm('^\\s+|\\s+$')%>% | |
srm('\\s','-') | |
} | |
get_stores <- function(uri){ | |
uri%>% | |
xml2::read_html()%>% | |
rvest::html_nodes(xpath='//p[contains(@class, "store-address")]')%>% | |
rvest::html_text()%>% | |
srm() | |
} | |
register_google(key = Sys.getenv('GOOGLE_KEY')) | |
uri_root <- 'https://www.cvs.com/store-locator/cvs-pharmacy-locations' | |
cvs <- tibble::tibble(state = gsub('\\s','-',state.name)) | |
cvs_cities <- cvs%>% | |
dplyr::mutate( | |
state_uri = glue::glue('{uri_root}/{state}'), | |
city = purrr::map(state_uri,purrrogress::with_progress(get_cities,total = 50)) | |
)%>% | |
tidyr::unnest() | |
this <- 'West-Virginia' | |
cvs_stores <- cvs_cities%>% | |
dplyr::filter(state==this)%>% | |
dplyr::mutate( | |
store = purrr::map(glue::glue('{uri_root}/{state}/{city}'), | |
purrrogress::with_progress(get_stores, | |
total = cvs_cities%>% | |
dplyr::filter(state==this)%>% | |
nrow())) | |
)%>% | |
tidyr::unnest()%>% | |
ggmap::mutate_geocode(store) | |
m <- ggmap::get_map( | |
location = this%>%srm('-',' '), | |
zoom = 7, | |
scale = "auto", | |
maptype = "toner-lite", | |
source = "stamen") | |
m%>% | |
ggmap::ggmap( | |
extent = "device", | |
legend = "topright" | |
) + | |
ggplot2::geom_point( | |
data = cvs_stores, | |
aes(x = lon, y = lat), | |
colour='red' | |
) + | |
labs(title = glue::glue('CVS store location {this}')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment