使用rbind.pages时从JSON填写api列表的空白参数?

时间:2016-11-09 19:21:04

标签: json r api foursquare jsonlite

在"搜索"上解决foursquare API给定经度和纬度点的某些地方的数据。在拉动期间,有一个部分有"类别'那给了我麻烦。我一直在使用之前提出的问题的建议: unest list with nested data frames in R

然而,那是使用"探索"来自FS的API,它完美地运作。现在我正在使用"搜索" API和一些lat长点在没有类别的情况下拉入嵌套数据框:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 30, 29

我依赖于cbind,因为id与合并不兼容。我如何解释这些零,并将该类别建立为“NA'所以当我绑定时,我不会收到以下错误?基本上,未列出的功能是杀死零数据帧,缩短我的列表以绑定

foursquare<-function(x,y,z,r){
    w<-paste("https://api.foursquare.com/v2/venues/search?ll=",x,
             "&radius=",r,"&oauth_token=",y,"&v=",z,sep="")
    u<-getURL(w)
    test<-fromJSON(u)
    {locationid =""
      locationname=""
      location =""
      lat=""
      long=""
      categories = ""
      checkinscount = ""
      userscount = ""
      beenhere=""
      herenow=""}
    for(n in 1:length(test$response$venues)) {
      #extract
      locationid = test$response$venues$id
      locationname = test$response$venues$name
      location= test$response$venues$location$address
      lat = test$response$venues$location$lat
      long = test$response$venues$location$lng
      categories= test$response$venues$categories
      checkinscount = test$response$venues$stats$checkinsCount
      userscount = test$response$venues$stats$usersCount
      beenhere = test$response$venues$beenHere$unconfirmedCount
      herenow = test$response$venues$hereNow$count


      search_api = as.data.frame(cbind(locationid, locationname, location, lat, long,
                                       checkinscount,userscount, beenhere, herenow))
      print(categories)
      categories = jsonlite::rbind.pages(categories)
      categories = categories[, c("name")]
      print(categories)
      search_api = as.data.frame(cbind(search_api, categories))
    }

    #add columns
    search_api$pulled_date = Sys.time()
    search_api$x_query = paste(x)
    search_api$y_query = y
    search_api$type_api = 'search'
    search_api$radius = paste(r)

    #prep for writeout
    time = gsub("[[:punct:]]", "", Sys.time())
    filename <- paste(time,"search_api",".csv", sep="") 
    print(filename)
    write.csv(search_api, file = filename)
  }

  foursquare("40.7575425406984,-73.9295267264121","auth_tok","20161027", 1000)

以下是我的代码:

{{1}}

1 个答案:

答案 0 :(得分:0)

类别是矢量吗?你可以填写NA吗?

if (length(categories) < 30) {categories <- c(categories, rep(NA, 30 - length(categories)))}