我一直在使用googleway软件包使用place\u type键检索信息,但我一直坚持使用60 results limit restriction.

我正在try 一种稍微不同的方法来解决这个限制:分而治之.

为了检索60个结果,我正在使用提供的卓越解决方案here,当我执行单数搜索时,一切都很好.

提前致谢,危地马拉致以最良好的问候

这里是我的纯文本坐标文件:

coords
"14.5446147628533, -90.84266666418"
"14.5538523714673, -90.84266666418"

这是我的代码:

###Preamble packages##
library(tidyverse)
library(googleway)
### load coordinates, plain text
dfCoords <- read.csv("~/coords.txt", sep="")
##Added For-loop begin##
for (i in dfCoords$coords) {
#### Original script begin ###
place_type <- "store" 
key <- c("API Key") 
radius <- 100 
location <- i

format_res <- function(res) {
  setNames(
    cbind(
      googleway::access_result(res, "coordinates"),
      googleway::access_result(res, "place_name")
    )
    , c("lat", "long", "name")
  )
}

do_search <- function(place_type, key, location, radius, page_token = NULL) {
  
  google_places(
    place_type = place_type,
    location = location,
    key = key,
    radius = radius,
    page_token = page_token
  )
}

full_search <- function(place_type, key, location, radius) {
  
  counter <- 0
  
  page_token <- NULL ## can start on NULL because it means we're doing the first query
  is_another_page <- TRUE 
  
  
  while( is_another_page ) {
    
    res <- do_search(place_type, key, location, radius, page_token)
    
    if( res$status == "OK" ) { ## check a valid result was returned
      
      if( counter == 0 ) {
        df <- format_res( res )
      } else {
        df <- rbind(df, format_res( res ) )
      }
      
      counter <- counter + 1
    }
    
    page_token <- res[["next_page_token"]]
    is_another_page <- !is.null( page_token )
    Sys.sleep(3)  ## Sleep the function before the next call because there's a time limit
  }
  return(df)
}

df <- full_search(place_type, key, location, radius)

##Original script ends

}
##Added for loop end

str( df )

intuition of the workaround

推荐答案

  1. 您只需在位置上循环,并从循环内部调用函数(否则您将在每次迭代中创建和定义函数)

  2. 我已经在format_res()中的结果中添加了place_id,因此您可以获得唯一的位置ID.在处理结果时,您将需要此选项,因为即使您指定了radius,google仍会为您提供超出此值的结果.

  3. 您需要将循环每次迭代的结果指定给对象.我已经为此创建了lst_results个列表

  4. 您给出的两个示例坐标不会产生任何结果,因此我添加了一些错误处理来解释从google返回的ZERO_RESULTS.我添加了第三个坐标对,向您展示它的工作原理.

以下是完整的更新代码


library(googleway)

format_res <- function(res) {
  setNames(
    cbind(
      googleway::access_result(res, "coordinates"),
      googleway::access_result(res, "place_name"),
      googleway::access_result(res, "place")       ## store the unique place_id as well
    )
    , c("lat", "long", "name", "place_id")
  )
}

do_search <- function(place_type, key, location, radius, page_token = NULL) {
  
  google_places(
    place_type = place_type,
    location = location,
    key = key,
    radius = radius,
    page_token = page_token
  )
  
}

full_search <- function(place_type, key, location, radius) {
  
  counter <- 0
  
  page_token <- NULL ## can start on NULL because it means we're doing the first query
  is_another_page <- TRUE 
  
  ## initialise a data.frame to store the results
  df <- data.frame(
    lat = vector("numeric", 0L)
    , long = vector("numeric", 0L)
    , name = vector("character", 0L)
    , place_id = vector("character", 0L)
    )
  
  while( is_another_page ) {
    
    res <- do_search(place_type, key, location, radius, page_token)
    
    if( res$status == "OK" ) { ## check a valid result was returned
      
      if( counter == 0 ) {
        df <- format_res( res )
      } else {
        df <- rbind(df, format_res( res ) )
      }
      
      counter <- counter + 1
    } else {
      ## print a message for not-OK results
      print(paste0(res[["status"]], " for ", paste0(location, collapse = ", ") ))
    }
    
    page_token <- res[["next_page_token"]]
    is_another_page <- !is.null( page_token )
    Sys.sleep(3)  ## Sleep the function before the next call because there's a time limit
  }
  return(df)
}

## I've added a 3rd example that actually has results
dfCoords <- data.frame(
  coords = c("14.5446147628533, -90.84266666418" ,"14.5538523714673, -90.84266666418", "-37.816660, 144.967092")
)

key <- secret::get_secret("GOOGLE")
place_type <- "store" 
radius <- 100 

## create a list to store the results
lst_results <- vector("list", length = nrow(dfCoords))
## Using a list will be more efficient that `rbind`-ing a data.frame in each iteration

## loop through the indexes of the coordinates
## this wy we can assign the results to the correct index of the list
for (i in 1:nrow(dfCoords) ) {
  
  location <- dfCoords[i, "coords"]
  
  ## the coordiantes must be a numeric vector
  location <- as.numeric(strsplit(location, ",")[[1]])

  
  lst_results[[ i ]] <- full_search(
    place_type = place_type
    , key = key
    , location = location
    , radius = radius
    )
}

lapply(lst_results, head)

# [[1]]
# [1] lat  long name
# <0 rows> (or 0-length row.names)
# 
# [[2]]
# [1] lat  long name
# <0 rows> (or 0-length row.names)
# 
# [[3]]
#         lat     long                                          name                    place_id
# 1 -37.81681 144.9665           StayCentral Flinders Lane Melbourne ChIJmy5Y5YxD1moRwnnrXIAiejM
# 2 -37.81601 144.9665 EB Games / ZiNG Pop Culture - Swanston Street ChIJz6n71LVC1moR-wgn04JtBjk
# 3 -37.81666 144.9668                     Tiffany Pollard Jewellery ChIJ45afhLZC1moRnyg_JBIEf2o
# 4 -37.81666 144.9668                                 dead & buried ChIJx_udg7ZC1moR2Kw-kXTvRIw
# 5 -37.81670 144.9667                          Citizen Watch Repair ChIJtW1Cx8lC1moRxJsUpo14NAY
# 6 -37.81671 144.9669                            Paris in Melbourne ChIJR_J5hLZC1moRxZ7EIUb5ZQw

R相关问答推荐

如何在热图中绘制一个图形,但在每个单元格中通过饼形图显示?

如何创建构成多个独立列条目列表的收件箱框列?

R的GG平行坐标图中的排序变量

如何使用shinyChatR包配置聊天机器人

如何求解arg必须为NULL或deSolve包的ode函数中的字符向量错误

如何计算前一行的值,直到达到标准?

如何编辑ggplot的图例字使用自定义对象(gtable)?'

如何在kableextra调用cell_spec()中忽略NA?

如何将移除事件分配给动态创建的按钮?

将包含卷的底部25%的组拆分为2行

在df中保留原始变量和新变量

将小数分隔符放在R中的前两位数字之后

在R中使用Scale_y_Break后更改y轴标签

如何使用tryCatch执行语句并忽略警告?

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

比较理论阿尔法和经验阿尔法

R中1到n_1,2到n_2,…,n到n_n的所有组合都是列表中的向量?

基于Key->Value数据帧的基因子集相关性提取

我已经运行了几个月的代码的`Palette()`中出现了新的gglot错误

每行不同列上的行求和