循环函数没有在 R

问题描述

我在下面创建了一个函数,用于从地方 api 中获取谷歌数据。输入是带有 api 键的 google url,应该返回搜索到的地方的数据集。但由于某种原因,数据集只返回相同的数据迭代(即一遍又一遍地相同的地方)。如果我手动逐行运行,它可以正常工作,所以我有点卡住了。我想可能和返回值有关。

因此该函数调用 api 并创建一个数据集(ds),然后抓取下一个页面键(页面令牌),因为 google 一次只发送 20 个值。然后运行循环,将下一页添加到 URL 并将其放入新数据集 (ds_new),然后通过 rbind(ds,ds_new) 将该数据与旧数据组合。这个循环一直持续到下一页键 == NULL 或运行十次迭代。然后返回数据集(ds)

任何提示或帮助将不胜感激。

google_key_places <- ###############
search_type <- "bar"
lat_long <- "-27.502870962086018,153.03244147954413"
radius <- "6000"    #in meters 
pagetoken <- ""
url <- paste0("https://maps.googleapis.com/maps/api/place/nearbysearch/json?","location=",lat_long,"&radius=",radius,"&type=",search_type,"&key=",google_key_places)
get_google_places <- function(url){
  
    doc <- getURL(url)                       # Grabs the URL data
    x <- jsonlite::fromJSON(doc)
    ds <- cbind(x$results$geometry$location,# lat and long
                x$results$name,# Name of Business
                x$results$vicinity,# Address of Business
                x$results$price_level,# Price Level (1 Cheapest - 5 Most Expensive)
                x$results$rating,# Google ratings
                x$results$user_ratings_total,# Total User ratings
                x$results$place_id)          # Google ID Code
    pagetoken <- x$next_page_token
    
    for (i in 1:10){ 
      url_new <- paste0(url,"&pagetoken=",pagetoken)
      doc <- getURL(url_new)
      x <- jsonlite::fromJSON(doc)
      ds_new <- cbind(x$results$geometry$location,# lat and long
                  x$results$name,# Name of Business
                  x$results$vicinity,# Address of Business
                  x$results$price_level,# Price Level (1 Cheapest - 5 Most Expensive)
                  x$results$rating,# Google ratings
                  x$results$user_ratings_total,# Total User ratings
                  x$results$place_id)          # Google ID Code
      pagetoken <- x$next_page_token
      ds <- rbind(ds,ds_new)
      if (is.null(pagetoken)){
        return(ds)
      }
    }
    
    return(ds)
  
}

解决方法

我会尝试这些通用步骤:

  1. 列个清单。我们首先简单地将列表命名为对您有意义的名称:

    google_stuff<-list()

  2. 接下来,在定义您的输出时,在循环内,您使用 [[]]。本质上,您是在 google_stuff 中为 10 个循环中的每一个创建一个列表项。因此,在基本形式中,您现在将拥有:

    google_stuff<-list()
    for (i in 1:10){
    url_new <- paste0(url,"&pagetoken=",pagetoken)
    doc <- getURL(url_new)
    x <- jsonlite::fromJSON(doc)
    ds_new <- cbind(x$results$geometry$location,# lat and long
               x$results$name,# Name of Business
               x$results$vicinity,# Address of Business
               x$results$price_level,# Price Level (1 Cheapest - 5 Most 
                                                              #Expensive)
               x$results$rating,# Google Ratings
               x$results$user_ratings_total,# Total User Ratings
                x$results$place_id)          # Google ID Code
     pagetoken <- x$next_page_token
    
    
     google_stuff[[i]]<-ds_new
    

    }

  3. 最后,您可以使用 dplyr::bind_rows(google_stuff,ds) 获取包含您提取的所有数据的漂亮数据框。

    all_my_google_stuff<-dplyr::bind_rows(google_stuff,ds)