I've a data frame with locations and I want to append phone numbers and websites to them from Google Places in a pipeable solution.
The closest I've gotten to doing this was using googleway
, unlisting the JSON and using regexes to extract the place id, and doing the same again for the phone numbers and email addresses. Is there a more targeted way of doing this?
library(googleway)
library(tidyverse)
set_key("api")
index_no <- c(1,2,3,4)
landmark<- c("Sydney Opera House","Eiffel Tower","Empire State Building","Big Ben")
df <- data.frame(index_no,landmark, stringsAsFactors = F)
df %>%
rowwise() %>%
# Place IDs are required for the function beneath
do(data.frame(., place_id = unlist(google_places(search_string = .$landmark)))) %>%
# Place IDs are 27 chars
filter(grepl("^\\S{27}$", place_id )) %>%
do(data.frame(., details = unlist(google_place_details(place_id = .$place_id )))) %>%
unique() %>%
# Gets non-Google URls and Phone Numbers
filter(grepl("(?!.*(google|maps))(^https?.*|^\\+\\d)", details, perl = T )) %>%
group_by(landmark) %>%
mutate(seq = 1:n()) %>%
spread(seq, details) %>%
rename(phone_number = `1`, website = `2`) %>%
select(-place_id) %>%
ungroup()