library(tidycensus)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(tmap)
library(jsonlite)
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()  masks stats::filter()
## ✖ purrr::flatten() masks jsonlite::flatten()
## ✖ dplyr::lag()     masks stats::lag()
library(httr)
library(jsonlite)
library(reshape2)
## 
## Attaching package: 'reshape2'
## 
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(here)
## here() starts at C:/Users/lvillaquiran3/CP-8883/CP8883
library(yelpr)
library(knitr)
tidycensus::census_api_key(Sys.getenv("censuscle"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
#test
tract <- suppressMessages(
  get_acs(geography = "tract", # or "block group", "county", "state" etc. 
          state = "FL",
          county = c("Seminole", "Orange"), 
          variables = c(hhincome = 'B19019_001',
                        race.tot = "B02001_001", 
                        race.white = "B02001_002", 
                        race.black = 'B02001_003'
          ),
          year = 2019,
          survey = "acs5", # American Community Survey 5-year estimate
          geometry = TRUE, # returns sf objects
          output = "wide") # wide vs. long
)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
message(sprintf("nrow: %s, ncol: %s", nrow(tract), ncol(tract)))
## nrow: 293, ncol: 11
tract %>% head() %>% knitr::kable()
GEOID NAME hhincomeE hhincomeM race.totE race.totM race.whiteE race.whiteM race.blackE race.blackM geometry
12095016902 Census Tract 169.02, Orange County, Florida 40394 6548 6000 514 2669 665 1392 413 MULTIPOLYGON (((-81.44418 2…
12095016804 Census Tract 168.04, Orange County, Florida 64205 7831 3714 606 2170 414 283 189 MULTIPOLYGON (((-81.37578 2…
12095012000 Census Tract 120, Orange County, Florida 30555 4059 8768 832 892 247 6612 868 MULTIPOLYGON (((-81.45219 2…
12117020805 Census Tract 208.05, Seminole County, Florida 112672 14485 9264 630 7160 634 576 221 MULTIPOLYGON (((-81.36338 2…
12117021506 Census Tract 215.06, Seminole County, Florida 83895 10557 3786 319 3085 405 184 152 MULTIPOLYGON (((-81.36238 2…
12095016411 Census Tract 164.11, Orange County, Florida 52156 15095 3111 370 2393 436 452 237 MULTIPOLYGON (((-81.27295 2…
tract <- tract %>% 
  select(GEOID, 
         hhincome = hhincomeE, # New name = old name
         race.tot = race.totE,
         race.white = race.whiteE,
         race.black = race.blackE)

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(tract) + tm_borders()
#getting ready for yelp
get_r <- function(poly, epsg_id){
  bb <- st_bbox(poly)
  bb_corner <- st_point(c(bb[1], bb[2])) %>% st_sfc(crs = epsg_id)
  bb_center_x <- (bb[3]+bb[1])/2
  bb_center_y <- (bb[4]+bb[2])/2
  bb_center <- st_point(c(bb_center_x, bb_center_y)) %>% st_sfc(crs = epsg_id) %>% st_sf()
  r <- st_distance(bb_corner, bb_center)
  bb_center$radius <- r*1.2
  return(bb_center)
}
epsg_id <- 4326
r4all_loop <- vector("list", nrow(tract))
for (i in 1:nrow(tract)){
  r4all_loop[[i]] <- tract %>% 
    st_transform(crs = epsg_id) %>% 
    st_geometry() %>% 
    .[[i]] %>% 
    get_r(epsg_id = epsg_id)
}

r4all_loop <- bind_rows(r4all_loop)

r4all_apply <- tract %>%
  st_geometry() %>% 
  st_transform(crs = epsg_id) %>% 
  lapply(., function(x) get_r(x, epsg_id = epsg_id))

r4all_apply <- bind_rows(r4all_apply)
identical(r4all_apply, r4all_loop)
## [1] TRUE
ready_4_yelp <- r4all_apply %>% 
  mutate(x = st_coordinates(.)[,1],
         y = st_coordinates(.)[,2])
tmap_mode('view')
## tmap mode set to interactive viewing
ready_4_yelp[1:10,] %>% 
  st_buffer(., dist = .$radius) %>% 
  tm_shape(.) + tm_polygons(alpha = 0.5, col = 'pink') +
  tm_shape(tract[1:10,]) + tm_borders(col= 'purple')
which_tract <- 1
beer_and_wine <- business_search(api_key = Sys.getenv('yelpcle'), # like we did for census, store your api key
                        categories = 'beer_and_wine', # return only restaurant businesses
                        latitude = ready_4_yelp$y[which_tract],
                        longitude = ready_4_yelp$x[which_tract],
                        offset = 0, # 1st page, 1st obs
                        radius = round(ready_4_yelp$radius[which_tract]), # radius requires integer value
                        limit = 50) # how many business per page
## No encoding supplied: defaulting to UTF-8.
lapply(beer_and_wine, head)
## $businesses
##                       id                                   alias
## 1 dkRaj0GQpnkjN92phdoj9Q                          bevfly-orlando
## 2 gkxcdJXZQ8BrwW-RFMgJtg              universal-liquor-orlando-3
## 3 0_5RsXpwaacwJUNM_J0ekA open-container-wine-tasting-bar-orlando
## 4 Ix9TK6coLr2UXv4cjK_N9w                  grand-liquor-orlando-3
## 5 j0f4wFUUiwJEP_hwVs1dhA    abc-fine-wine-and-spirits-orlando-14
## 6 llXWt7pADJX-q9XjK1It3g                    party-liquor-orlando
##                              name
## 1                          BevFly
## 2                Universal Liquor
## 3 Open Container Wine Tasting Bar
## 4                    Grand Liquor
## 5         ABC Fine Wine & Spirits
## 6                    Party Liquor
##                                                              image_url
## 1 https://s3-media3.fl.yelpcdn.com/bphoto/uHuj1qdCR9I-t9lV9emO8w/o.jpg
## 2 https://s3-media1.fl.yelpcdn.com/bphoto/iCBH65pWmQEry7lHTDLa2w/o.jpg
## 3 https://s3-media2.fl.yelpcdn.com/bphoto/WPoKFO12bV6F178WrsDMEg/o.jpg
## 4 https://s3-media4.fl.yelpcdn.com/bphoto/63cSbhtfFc-7nkFQQ3mVBg/o.jpg
## 5 https://s3-media2.fl.yelpcdn.com/bphoto/PACFTmKKUxlX1wT-XEE9DA/o.jpg
## 6                                                                     
##   is_closed
## 1     FALSE
## 2     FALSE
## 3     FALSE
## 4     FALSE
## 5     FALSE
## 6     FALSE
##                                                                                                                                                                                                    url
## 1                          https://www.yelp.com/biz/bevfly-orlando?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 2              https://www.yelp.com/biz/universal-liquor-orlando-3?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 3 https://www.yelp.com/biz/open-container-wine-tasting-bar-orlando?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 4                  https://www.yelp.com/biz/grand-liquor-orlando-3?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 5    https://www.yelp.com/biz/abc-fine-wine-and-spirits-orlando-14?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 6                    https://www.yelp.com/biz/party-liquor-orlando?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
##   review_count                          categories rating coordinates.latitude
## 1           14 beer_and_wine, Beer, Wine & Spirits    4.5             28.46257
## 2            4 beer_and_wine, Beer, Wine & Spirits    4.0             28.42595
## 3            2 beer_and_wine, Beer, Wine & Spirits    4.5             28.47608
## 4            7 beer_and_wine, Beer, Wine & Spirits    3.0             28.46259
## 5           10 beer_and_wine, Beer, Wine & Spirits    2.0             28.47327
## 6            2 beer_and_wine, Beer, Wine & Spirits    2.0             28.48651
##   coordinates.longitude     transactions price         location.address1
## 1             -81.45404 pickup, delivery    $$     5425 International Dr
## 2             -81.44300                   <NA>       9938 Universal Blvd
## 3             -81.44940                   <NA>     4951 International Dr
## 4             -81.45633 pickup, delivery  <NA>     5601 International Dr
## 5             -81.39588                     $$ 5895 S Orange Blossom Trl
## 6             -81.40867 pickup, delivery     $       2200 Americana Blvd
##   location.address2 location.address3 location.city location.zip_code
## 1              <NA>                         Orlando             32819
## 2           Ste 100                         Orlando             32819
## 3          Unit 142                         Orlando             32819
## 4              <NA>                         Orlando             32819
## 5                                           Orlando             32839
## 6                                           Orlando             32839
##   location.country location.state
## 1               US             FL
## 2               US             FL
## 3               US             FL
## 4               US             FL
## 5               US             FL
## 6               US             FL
##                             location.display_address        phone
## 1           5425 International Dr, Orlando, FL 32819 +14077514689
## 2    9938 Universal Blvd, Ste 100, Orlando, FL 32819             
## 3 4951 International Dr, Unit 142, Orlando, FL 32819 +14078683518
## 4           5601 International Dr, Orlando, FL 32819 +14077514517
## 5       5895 S Orange Blossom Trl, Orlando, FL 32839 +14078554040
## 6             2200 Americana Blvd, Orlando, FL 32839 +14078502069
##    display_phone distance
## 1 (407) 751-4689 3386.626
## 2                3786.702
## 3 (407) 868-3518 3822.064
## 4 (407) 751-4517 3711.727
## 5 (407) 855-4040 3278.163
## 6 (407) 850-2069 3895.634
## 
## $total
## [1] 6
## 
## $region
## $region$center
## $region$center$longitude
## [1] -81.42042
## 
## $region$center$latitude
## [1] 28.45301
which_tract <- 1
health <- business_search(api_key = Sys.getenv('yelpcle'), # like we did for census, store your api key
                        categories = 'health', # return only health businesses
                        latitude = ready_4_yelp$y[which_tract],
                        longitude = ready_4_yelp$x[which_tract],
                        offset = 0, # 1st page, 1st obs
                        radius = round(ready_4_yelp$radius[which_tract]), # radius requires integer value
                        limit = 50) # how many business per page
## No encoding supplied: defaulting to UTF-8.
lapply(health, head)
## $businesses
##                       id                                 alias
## 1 gWX6ZmBhXQvlo6f9SH7ung        southpark-dental-group-orlando
## 2 Fi5zAVKutTwL0jQMA-X8LA    carlos-j-gurreonero-dmd-pa-orlando
## 3 qMX50FWGEkRn7LZpsmHZyg                    oneblood-orlando-2
## 4 k1sFFvGZevfbyz0HtCzrvQ universal-day-spa-and-massage-orlando
## 5 OyTcL2026sRnE0YBTvy1dw      balancing-point-wellness-orlando
## 6 aS5CnPApP1NSxLEsWebjpA      hahns-acupuncture-clinic-orlando
##                          name
## 1      Southpark Dental Group
## 2 Carlos J Gurreonero DMD, PA
## 3                    OneBlood
## 4 Universal Day Spa & Massage
## 5    Balancing Point Wellness
## 6   Hahn's Acupuncture Clinic
##                                                              image_url
## 1 https://s3-media3.fl.yelpcdn.com/bphoto/_lIuBHA8icLwPouIDLb2tw/o.jpg
## 2 https://s3-media3.fl.yelpcdn.com/bphoto/D35u8JyohujCwg0xP07-Qw/o.jpg
## 3 https://s3-media1.fl.yelpcdn.com/bphoto/Mnm5HkPK9iatuN4rK0k_AA/o.jpg
## 4 https://s3-media2.fl.yelpcdn.com/bphoto/i6U-w0Lran7FlKlSlNp7Yw/o.jpg
## 5 https://s3-media1.fl.yelpcdn.com/bphoto/5CPOXSdZCcyh5QuwQd0XpQ/o.jpg
## 6 https://s3-media2.fl.yelpcdn.com/bphoto/g4KKpU16fTj9A20_guhecw/o.jpg
##   is_closed
## 1     FALSE
## 2     FALSE
## 3     FALSE
## 4     FALSE
## 5     FALSE
## 6     FALSE
##                                                                                                                                                                                                  url
## 1        https://www.yelp.com/biz/southpark-dental-group-orlando?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 2    https://www.yelp.com/biz/carlos-j-gurreonero-dmd-pa-orlando?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 3                    https://www.yelp.com/biz/oneblood-orlando-2?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 4 https://www.yelp.com/biz/universal-day-spa-and-massage-orlando?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 5      https://www.yelp.com/biz/balancing-point-wellness-orlando?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
## 6      https://www.yelp.com/biz/hahns-acupuncture-clinic-orlando?adjust_creative=5BlG8wDTfR3WgPVio67Lrw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=5BlG8wDTfR3WgPVio67Lrw
##   review_count
## 1           78
## 2           16
## 3           13
## 4           48
## 5           18
## 6           19
##                                                                                               categories
## 1 periodontists, generaldentistry, cosmeticdentists, Periodontists, General Dentistry, Cosmetic Dentists
## 2           cosmeticdentists, oralsurgeons, endodontists, Cosmetic Dentists, Oral Surgeons, Endodontists
## 3                                                         blooddonation, Blood & Plasma Donation Centers
## 4                           massage, reflexology, massage_therapy, Massage, Reflexology, Massage Therapy
## 5                                            acupuncture, tcm, Acupuncture, Traditional Chinese Medicine
## 6                                                                               acupuncture, Acupuncture
##   rating coordinates.latitude coordinates.longitude transactions
## 1    4.5             28.44368             -81.42861         NULL
## 2    5.0             28.44236             -81.42846         NULL
## 3    5.0             28.44926             -81.43031         NULL
## 4    4.0             28.42665             -81.44451         NULL
## 5    5.0             28.45158             -81.38471         NULL
## 6    5.0             28.45964             -81.45610         NULL
##        location.address1 location.address2 location.address3 location.city
## 1     8801 Commodity Cir                                           Orlando
## 2     8865 Commodity Cir             Ste 6                         Orlando
## 3     8669 Commodity Cir                                           Orlando
## 4    9938 Universal Blvd           Ste 132              <NA>       Orlando
## 5       7726 Winegard Rd         2nd Floor                         Orlando
## 6 7021 Grand National Dr           Ste 105                         Orlando
##   location.zip_code location.country location.state
## 1             32819               US             FL
## 2             32819               US             FL
## 3             32819               US             FL
## 4             32819               US             FL
## 5             32809               US             FL
## 6             32819               US             FL
##                             location.display_address        phone
## 1              8801 Commodity Cir, Orlando, FL 32819 +14072480100
## 2       8865 Commodity Cir, Ste 6, Orlando, FL 32819 +14072195931
## 3              8669 Commodity Cir, Orlando, FL 32819 +14072485009
## 4    9938 Universal Blvd, Ste 132, Orlando, FL 32819 +14073344450
## 5     7726 Winegard Rd, 2nd Floor, Orlando, FL 32809 +14072432446
## 6 7021 Grand National Dr, Ste 105, Orlando, FL 32819 +13212789617
##    display_phone distance price
## 1 (407) 248-0100 1294.390  <NA>
## 2 (407) 219-5931 1421.151  <NA>
## 3 (407) 248-5009 1052.971  <NA>
## 4 (407) 334-4450 3786.102    $$
## 5 (407) 243-2446 3500.265  <NA>
## 6 (321) 278-9617 3565.571  <NA>
## 
## $total
## [1] 108
## 
## $region
## $region$center
## $region$center$longitude
## [1] -81.42042
## 
## $region$center$latitude
## [1] 28.45301
# FUNCTION
get_yelp <- function(tract, category){
  # ----------------------------------
  # Gets one row of tract information (1,) and category name (str),
  # Outputs a list of business data.frame
  n <- 1
  # First request --------------------------------------------------------------
  resp <- business_search(api_key = Sys.getenv("yelpcle"), 
                          categories = category, 
                          latitude = tract$y, 
                          longitude = tract$x, 
                          offset = (n - 1) * 50, # = 0 when n = 1
                          radius = round(tract$radius), 
                          limit = 50)
  # Calculate how many requests are needed in total
  required_n <- ceiling(resp$total/50)
  
  # out is where the results will be appended to.
  out <- vector("list", required_n)
  
  # Store the business information to nth slot in out
  out[[n]] <- resp$businesses
  
  # Change the name of the elements to the total required_n
  # This is to know if there are more than 1000 businesses,
  # we know how many.
  names(out)[n] <- required_n
  
  # Throw error if more than 1000
  if (resp$total >= 1000)
  {
    # glue formats string by inserting {n} with what's currently stored in object n.
    print(glue::glue("{n}th row has >= 1000 businesses."))
    # Stop before going into the loop because we need to
    # break down Census Tract to something smaller.
    return(out)
  } 
  else 
  {
    # add 1 to n
    n <- n + 1
    
    # Now we know required_n -----------------------------------------------------
    # Starting a loop
    while(n <= required_n){
      resp <- business_search(api_key = Sys.getenv("yelpcle"), 
                              categories = category, 
                              latitude = tract$y, 
                              longitude = tract$x, 
                              offset = (n - 1) * 50, 
                              radius = round(tract$radius), 
                              limit = 50)
      
      out[[n]] <- resp$businesses
      
      n <- n + 1
    } #<< end of while loop
    
    # Merge all elements in the list into a single data frame
    out <- out %>% bind_rows()
    
    return(out)
  }
}
yelp_first_tract <- get_yelp(ready_4_yelp[1,], "health") %>% 
  as_tibble()
## No encoding supplied: defaulting to UTF-8.
## No encoding supplied: defaulting to UTF-8.
## No encoding supplied: defaulting to UTF-8.
yelp_first_tract %>% print
## # A tibble: 108 × 16
##    id           alias name  image…¹ is_cl…² url   revie…³ categ…⁴ rating coord…⁵
##    <chr>        <chr> <chr> <chr>   <lgl>   <chr>   <int> <list>   <dbl>   <dbl>
##  1 gWX6ZmBhXQv… sout… Sout… "https… FALSE   http…      78 <df>       4.5    28.4
##  2 Fi5zAVKutTw… carl… Carl… "https… FALSE   http…      16 <df>       5      28.4
##  3 qMX50FWGEkR… oneb… OneB… "https… FALSE   http…      13 <df>       5      28.4
##  4 k1sFFvGZevf… univ… Univ… "https… FALSE   http…      48 <df>       4      28.4
##  5 OyTcL2026sR… bala… Bala… "https… FALSE   http…      18 <df>       5      28.5
##  6 aS5CnPApP1N… hahn… Hahn… "https… FALSE   http…      19 <df>       5      28.5
##  7 YRluV6zSbxA… bj-h… BJ H… "https… FALSE   http…      17 <df>       4.5    28.4
##  8 KqfpjmSZoXq… stri… Stri… ""      FALSE   http…      11 <df>       5      28.4
##  9 hvE1yTtFhSK… glam… Glam… "https… FALSE   http…       6 <df>       5      28.4
## 10 wxrB2ozWsvt… clas… Clas… "https… FALSE   http…      11 <df>       4.5    28.4
## # … with 98 more rows, 7 more variables: coordinates$longitude <dbl>,
## #   transactions <list>, location <df[,8]>, phone <chr>, display_phone <chr>,
## #   distance <dbl>, price <chr>, and abbreviated variable names ¹​image_url,
## #   ²​is_closed, ³​review_count, ⁴​categories, ⁵​coordinates$latitude
## # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
yelp_all_list <- vector("list", nrow(ready_4_yelp))

# Looping through all Census Tracts
for (row in 1:nrow(ready_4_yelp)){
  yelp_all_list[[row]] <- suppressMessages(get_yelp(ready_4_yelp[row,], "health" ))
  if (row %% 5 == 0){
    Sys.sleep(10)
  }
}
## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs
# Collapsing the list into a data.frame
yelp_all <- yelp_all_list %>% bind_rows() %>% as_tibble()
#Save the data you downloaded
yelp_all %>% write_rds("C:/Users/lvillaquiran3/CP-8883/CP8883/yelp_all.rds")
# print
yelp_all %>% print(width=1000)
## # A tibble: 32,591 × 16
##    id                     alias                                             
##    <chr>                  <chr>                                             
##  1 gWX6ZmBhXQvlo6f9SH7ung southpark-dental-group-orlando                    
##  2 Fi5zAVKutTwL0jQMA-X8LA carlos-j-gurreonero-dmd-pa-orlando                
##  3 qMX50FWGEkRn7LZpsmHZyg oneblood-orlando-2                                
##  4 k1sFFvGZevfbyz0HtCzrvQ universal-day-spa-and-massage-orlando             
##  5 OyTcL2026sRnE0YBTvy1dw balancing-point-wellness-orlando                  
##  6 aS5CnPApP1NSxLEsWebjpA hahns-acupuncture-clinic-orlando                  
##  7 YRluV6zSbxAKLc-c7_i3Yg bj-healthcare-massage-and-spa-orlando             
##  8 KqfpjmSZoXqXRpzrgBodUg stride-physical-therapy-and-rehabilitation-orlando
##  9 hvE1yTtFhSKxdAkVqH3fhw glam-by-sam-g-orlando                             
## 10 wxrB2ozWsvtpv7KYs7RKfQ class-act-massage-and-facial-spa-orlando          
##    name                                    
##    <chr>                                   
##  1 Southpark Dental Group                  
##  2 Carlos J Gurreonero DMD, PA             
##  3 OneBlood                                
##  4 Universal Day Spa & Massage             
##  5 Balancing Point Wellness                
##  6 Hahn's Acupuncture Clinic               
##  7 BJ Healthcare Massage & Spa             
##  8 Stride Physical Therapy & Rehabilitation
##  9 Glam By Sam G                           
## 10 Class Act Massage and Facial Spa        
##    image_url                                                             
##    <chr>                                                                 
##  1 "https://s3-media3.fl.yelpcdn.com/bphoto/_lIuBHA8icLwPouIDLb2tw/o.jpg"
##  2 "https://s3-media3.fl.yelpcdn.com/bphoto/D35u8JyohujCwg0xP07-Qw/o.jpg"
##  3 "https://s3-media1.fl.yelpcdn.com/bphoto/Mnm5HkPK9iatuN4rK0k_AA/o.jpg"
##  4 "https://s3-media2.fl.yelpcdn.com/bphoto/i6U-w0Lran7FlKlSlNp7Yw/o.jpg"
##  5 "https://s3-media1.fl.yelpcdn.com/bphoto/5CPOXSdZCcyh5QuwQd0XpQ/o.jpg"
##  6 "https://s3-media2.fl.yelpcdn.com/bphoto/g4KKpU16fTj9A20_guhecw/o.jpg"
##  7 "https://s3-media2.fl.yelpcdn.com/bphoto/G5Mze-CKvPXvrlYZE6XPCQ/o.jpg"
##  8 ""                                                                    
##  9 "https://s3-media2.fl.yelpcdn.com/bphoto/sfsahtVnkBdi4-7p0G5H-Q/o.jpg"
## 10 "https://s3-media2.fl.yelpcdn.com/bphoto/WXAu4-XL6eNCPeMTLj9Mqg/o.jpg"
##    is_closed
##    <lgl>    
##  1 FALSE    
##  2 FALSE    
##  3 FALSE    
##  4 FALSE    
##  5 FALSE    
##  6 FALSE    
##  7 FALSE    
##  8 FALSE    
##  9 FALSE    
## 10 FALSE    
##    url                                                                          
##    <chr>                                                                        
##  1 https://www.yelp.com/biz/southpark-dental-group-orlando?adjust_creative=5BlG…
##  2 https://www.yelp.com/biz/carlos-j-gurreonero-dmd-pa-orlando?adjust_creative=…
##  3 https://www.yelp.com/biz/oneblood-orlando-2?adjust_creative=5BlG8wDTfR3WgPVi…
##  4 https://www.yelp.com/biz/universal-day-spa-and-massage-orlando?adjust_creati…
##  5 https://www.yelp.com/biz/balancing-point-wellness-orlando?adjust_creative=5B…
##  6 https://www.yelp.com/biz/hahns-acupuncture-clinic-orlando?adjust_creative=5B…
##  7 https://www.yelp.com/biz/bj-healthcare-massage-and-spa-orlando?adjust_creati…
##  8 https://www.yelp.com/biz/stride-physical-therapy-and-rehabilitation-orlando?…
##  9 https://www.yelp.com/biz/glam-by-sam-g-orlando?adjust_creative=5BlG8wDTfR3Wg…
## 10 https://www.yelp.com/biz/class-act-massage-and-facial-spa-orlando?adjust_cre…
##    review_count categories   rating coordinates$latitude $longitude transactions
##           <int> <list>        <dbl>                <dbl>      <dbl> <list>      
##  1           78 <df [3 × 2]>    4.5                 28.4      -81.4 <list [0]>  
##  2           16 <df [3 × 2]>    5                   28.4      -81.4 <list [0]>  
##  3           13 <df [1 × 2]>    5                   28.4      -81.4 <list [0]>  
##  4           48 <df [3 × 2]>    4                   28.4      -81.4 <list [0]>  
##  5           18 <df [2 × 2]>    5                   28.5      -81.4 <list [0]>  
##  6           19 <df [1 × 2]>    5                   28.5      -81.5 <list [0]>  
##  7           17 <df [3 × 2]>    4.5                 28.4      -81.4 <list [0]>  
##  8           11 <df [2 × 2]>    5                   28.4      -81.5 <list [0]>  
##  9            6 <df [3 × 2]>    5                   28.4      -81.4 <list [0]>  
## 10           11 <df [3 × 2]>    4.5                 28.4      -81.4 <list [0]>  
##    location$address1      $address2   $address3 $city   $zip_code $country
##    <chr>                  <chr>       <chr>     <chr>   <chr>     <chr>   
##  1 8801 Commodity Cir     ""          ""        Orlando 32819     US      
##  2 8865 Commodity Cir     "Ste 6"     ""        Orlando 32819     US      
##  3 8669 Commodity Cir     ""          ""        Orlando 32819     US      
##  4 9938 Universal Blvd    "Ste 132"   <NA>      Orlando 32819     US      
##  5 7726 Winegard Rd       "2nd Floor" ""        Orlando 32809     US      
##  6 7021 Grand National Dr "Ste 105"   ""        Orlando 32819     US      
##  7 8865 Commodity Cir     "Ste 7B"    ""        Orlando 32819     US      
##  8 7932 W Sand Lake Rd    "Ste 203"   ""        Orlando 32819     US      
##  9 8803 Futures Dr        ""          ""        Orlando 32819     US      
## 10 1187 Florida Mall Ave  "Ste 120"   ""        Orlando 32809     US      
##    $state $display_address phone        display_phone  distance price
##    <chr>  <list>           <chr>        <chr>             <dbl> <chr>
##  1 FL     <chr [2]>        +14072480100 (407) 248-0100    1294. <NA> 
##  2 FL     <chr [3]>        +14072195931 (407) 219-5931    1421. <NA> 
##  3 FL     <chr [2]>        +14072485009 (407) 248-5009    1053. <NA> 
##  4 FL     <chr [3]>        +14073344450 (407) 334-4450    3786. $$   
##  5 FL     <chr [3]>        +14072432446 (407) 243-2446    3500. <NA> 
##  6 FL     <chr [3]>        +13212789617 (321) 278-9617    3566. <NA> 
##  7 FL     <chr [3]>        +14078616966 (407) 861-6966    1421. <NA> 
##  8 FL     <chr [3]>        +14073559820 (407) 355-9820    2000. <NA> 
##  9 FL     <chr [2]>        +13045618833 (304) 561-8833    1519. <NA> 
## 10 FL     <chr [3]>        +14076017000 (407) 601-7000    2516. <NA> 
## # … with 32,581 more rows
## # ℹ Use `print(n = ...)` to see more rows
yelp_all_alcohol <- vector("list", nrow(ready_4_yelp))

# Looping through all Census Tracts
for (row in 1:nrow(ready_4_yelp)){
  yelp_all_alcohol[[row]] <- suppressMessages(get_yelp(ready_4_yelp[row,], "beer_and_wine" ))
  if (row %% 5 == 0){
    Sys.sleep(10)
  }
}
## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs

## Warning: Outer names are only allowed for unnamed scalar atomic inputs
yelp_all_alcohol <- yelp_all_alcohol %>% bind_rows() %>% as_tibble()
#Save the data you downloaded
yelp_all_alcohol %>% write_rds("C:/Users/lvillaquiran3/CP-8883/CP8883/yelp_all_alcohol.rds")
# print
yelp_all_alcohol %>% print(width=1000)
## # A tibble: 942 × 16
##    id                     alias                                  
##    <chr>                  <chr>                                  
##  1 dkRaj0GQpnkjN92phdoj9Q bevfly-orlando                         
##  2 gkxcdJXZQ8BrwW-RFMgJtg universal-liquor-orlando-3             
##  3 0_5RsXpwaacwJUNM_J0ekA open-container-wine-tasting-bar-orlando
##  4 Ix9TK6coLr2UXv4cjK_N9w grand-liquor-orlando-3                 
##  5 j0f4wFUUiwJEP_hwVs1dhA abc-fine-wine-and-spirits-orlando-14   
##  6 llXWt7pADJX-q9XjK1It3g party-liquor-orlando                   
##  7 qF5FIrYXNcW7W43oj4ZnCA airport-smoke-shop-orlando             
##  8 6m4YXCVKbWqiLjHLU9lGMA abc-fine-wine-and-spirits-orlando-18   
##  9 btVPgGjHwrwK-GG4KfpMpQ abc-fine-wine-and-spirits-orlando-4    
## 10 JwYnWhwTD89K7FWsnuf7pg world-market-sanford-2                 
##    name                           
##    <chr>                          
##  1 BevFly                         
##  2 Universal Liquor               
##  3 Open Container Wine Tasting Bar
##  4 Grand Liquor                   
##  5 ABC Fine Wine & Spirits        
##  6 Party Liquor                   
##  7 Airport Smoke Shop             
##  8 ABC Fine Wine & Spirits        
##  9 ABC Fine Wine & Spirits        
## 10 World Market                   
##    image_url                                                             
##    <chr>                                                                 
##  1 "https://s3-media3.fl.yelpcdn.com/bphoto/uHuj1qdCR9I-t9lV9emO8w/o.jpg"
##  2 "https://s3-media1.fl.yelpcdn.com/bphoto/iCBH65pWmQEry7lHTDLa2w/o.jpg"
##  3 "https://s3-media2.fl.yelpcdn.com/bphoto/WPoKFO12bV6F178WrsDMEg/o.jpg"
##  4 "https://s3-media4.fl.yelpcdn.com/bphoto/63cSbhtfFc-7nkFQQ3mVBg/o.jpg"
##  5 "https://s3-media2.fl.yelpcdn.com/bphoto/PACFTmKKUxlX1wT-XEE9DA/o.jpg"
##  6 ""                                                                    
##  7 "https://s3-media4.fl.yelpcdn.com/bphoto/hvrwZIAP-Co1QhaAyals5g/o.jpg"
##  8 "https://s3-media2.fl.yelpcdn.com/bphoto/Q4B8xLwrnCRe0JIAPpDouw/o.jpg"
##  9 ""                                                                    
## 10 "https://s3-media2.fl.yelpcdn.com/bphoto/3QAm4Zhfw0UsHoCah4gDhQ/o.jpg"
##    is_closed
##    <lgl>    
##  1 FALSE    
##  2 FALSE    
##  3 FALSE    
##  4 FALSE    
##  5 FALSE    
##  6 FALSE    
##  7 FALSE    
##  8 FALSE    
##  9 FALSE    
## 10 FALSE    
##    url                                                                          
##    <chr>                                                                        
##  1 https://www.yelp.com/biz/bevfly-orlando?adjust_creative=5BlG8wDTfR3WgPVio67L…
##  2 https://www.yelp.com/biz/universal-liquor-orlando-3?adjust_creative=5BlG8wDT…
##  3 https://www.yelp.com/biz/open-container-wine-tasting-bar-orlando?adjust_crea…
##  4 https://www.yelp.com/biz/grand-liquor-orlando-3?adjust_creative=5BlG8wDTfR3W…
##  5 https://www.yelp.com/biz/abc-fine-wine-and-spirits-orlando-14?adjust_creativ…
##  6 https://www.yelp.com/biz/party-liquor-orlando?adjust_creative=5BlG8wDTfR3WgP…
##  7 https://www.yelp.com/biz/airport-smoke-shop-orlando?adjust_creative=5BlG8wDT…
##  8 https://www.yelp.com/biz/abc-fine-wine-and-spirits-orlando-18?adjust_creativ…
##  9 https://www.yelp.com/biz/abc-fine-wine-and-spirits-orlando-4?adjust_creative…
## 10 https://www.yelp.com/biz/world-market-sanford-2?adjust_creative=5BlG8wDTfR3W…
##    review_count categories   rating coordinates$latitude $longitude transactions
##           <int> <list>        <dbl>                <dbl>      <dbl> <list>      
##  1           14 <df [1 × 2]>    4.5                 28.5      -81.5 <chr [2]>   
##  2            4 <df [1 × 2]>    4                   28.4      -81.4 <chr [0]>   
##  3            2 <df [1 × 2]>    4.5                 28.5      -81.4 <chr [0]>   
##  4            7 <df [1 × 2]>    3                   28.5      -81.5 <chr [2]>   
##  5           10 <df [1 × 2]>    2                   28.5      -81.4 <chr [0]>   
##  6            2 <df [1 × 2]>    2                   28.5      -81.4 <chr [2]>   
##  7           10 <df [3 × 2]>    3.5                 28.5      -81.3 <list [0]>  
##  8            1 <df [1 × 2]>    5                   28.4      -81.4 <list [0]>  
##  9            1 <df [1 × 2]>    1                   28.6      -81.4 <list [0]>  
## 10           49 <df [3 × 2]>    4.5                 28.8      -81.3 <chr [0]>   
##    price location$address1         $address2  $address3 $city   $zip_code
##    <chr> <chr>                     <chr>      <chr>     <chr>   <chr>    
##  1 $$    5425 International Dr      <NA>      ""        Orlando 32819    
##  2 <NA>  9938 Universal Blvd       "Ste 100"  ""        Orlando 32819    
##  3 <NA>  4951 International Dr     "Unit 142" ""        Orlando 32819    
##  4 <NA>  5601 International Dr      <NA>      ""        Orlando 32819    
##  5 $$    5895 S Orange Blossom Trl ""         ""        Orlando 32839    
##  6 $     2200 Americana Blvd       ""         ""        Orlando 32839    
##  7 <NA>  5749 T G Lee Blvd         ""         <NA>      Orlando 32822    
##  8 $$    9001 S Orange Ave          <NA>      <NA>      Orlando 32824    
##  9 $$    4780 W Colonial Dr        ""         ""        Orlando 32808    
## 10 $$    2251 Wp Ball Blvd         ""         ""        Sanford 32771    
##    $country $state $display_address phone          display_phone    distance
##    <chr>    <chr>  <list>           <chr>          <chr>               <dbl>
##  1 US       FL     <chr [2]>        "+14077514689" "(407) 751-4689"    3387.
##  2 US       FL     <chr [3]>        ""             ""                  3787.
##  3 US       FL     <chr [3]>        "+14078683518" "(407) 868-3518"    3822.
##  4 US       FL     <chr [2]>        "+14077514517" "(407) 751-4517"    3712.
##  5 US       FL     <chr [2]>        "+14078554040" "(407) 855-4040"    3278.
##  6 US       FL     <chr [2]>        "+14078502069" "(407) 850-2069"    3896.
##  7 US       FL     <chr [2]>        "+14072404960" "(407) 240-4960"    6231.
##  8 US       FL     <chr [2]>        "+14078510000" "(407) 851-0000"    2043.
##  9 US       FL     <chr [2]>        "+14072930550" "(407) 293-0550"    1484.
## 10 US       FL     <chr [2]>        "+14073020263" "(407) 302-0263"    3323.
## # … with 932 more rows
## # ℹ Use `print(n = ...)` to see more rows
# Extracting geographic coordinates for all health businesses. 
yelp_health <- yelp_all %>% 
  mutate(x = .$coordinates$longitude,
         y = .$coordinates$latitude) %>% 
  filter(!is.na(x) & !is.na(y)) %>% 
  st_as_sf(coords = c("x", "y"), crs = 4326)
# Map1 spatially projects the Yelp health business data.
tm_shape(yelp_health) +
  tm_dots(col = "review_count", style="quantile")
# Extracting geographic coordinates for all beer and wine ("alcohol") businesses. 
yelp_alcohol <- yelp_all_alcohol %>% 
  mutate(x = .$coordinates$longitude,
         y = .$coordinates$latitude) %>% 
  filter(!is.na(x) & !is.na(y)) %>% 
  st_as_sf(coords = c("x", "y"), crs = 4326)
# Map 2 Spatially Projects the beer and wine business data. 
tm_shape(yelp_alcohol) +
  tm_dots(col = "review_count", style="quantile")