我有一个带有位置的数据框,我想在一个可管道的解决方案中从 Google Places 向它们附加电话号码和网站。
我最接近的方法是使用googleway
,取消列出 JSON 并使用正则表达式提取地点 ID,然后对电话号码和电子邮件地址再次执行相同操作。有没有更有针对性的方法来做到这一点?
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()