【发布时间】:2019-06-12 16:37:12
【问题描述】:
我有一个包含位置的数据框,我想在 pipeable 解决方案中从 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()
【问题讨论】:
-
查看
?access_result看看是否已经有访问器来获取您想要的信息。如果您想从 API 获得特定结果,您可以随时 add it 或 request it
标签: r json dplyr tidyverse googleway