R - 将 Google Places API 数据放入数据框中

问题描述 投票:0回答:2

我已经成功使用 Google Places API 提取了我当地的所有餐馆,但我现在正在努力将这些放入数据框(我的最终目标是将其导出到 Excel)。

我有以下代码:

myPlaces <- google_places(location = myLocation, 
        place_type = "restaurant",
                        radius = 500,
                        key = key)

nextPlaces <- google_places(location = myLocation,
                            radius = 500,
                place_type = "restaurant",
                            page_token = myPlaces$next_page_token, 
                            key = key)

nextPlaces2 <- google_places(location = myLocation,
                            radius = 500,
                place_type = "restaurant",
                            page_token = nextPlaces$next_page_token, 
                            key = key)

这为我提供了我想要的数据,但是当我尝试用它创建一个数据框时,我失败了:

> df1 <- select(myPlaces, name, business_status, rating, types, price_level, geometry.location.lat, geometry.location.lng)

收到错误消息“错误:

select()
不处理列表。”

我对 R 相当陌生,我花了很多时间才达到这一点,我觉得我已经接近我的最终目标,但对此感到困惑。非常感谢任何帮助。

谢谢,

r list dataframe google-places-api
2个回答
0
投票

您可以写:

myPlaces<- myPlaces$results

nextPlaces<- nextPlaces$results

nextPlaces2<- nextPlaces2$results

0
投票

您可以使用

newplaces
,这是 Google Places API 的 R 包装程序包(新 API 于 2023 年底发布)。

在您的示例中,您可以运行以下命令:

# Install package from github:
remotes::install_github("aTnT/newplaces")

library(newplaces)

# Set your API key:
set_api_key(key = "YOUR_MAPS_API_KEY")

# Get restaurants within a 500 m radius around a location:
myPlaces <- text_search(textQuery = "restaurants", location = "bias", circle_center_latitude = 40.75797, circle_center_longitude = -73.98554, circle_radius = 500)

您可以通过

myPlaces$places
:

的位置访问 tibble 数据框
myPlaces$places

# A tbl_json: 20 x 77 tibble with a "JSON" attribute
   ..JSON document.id name  id    nationalPhoneNumber internationalPhoneNu…¹ formattedAddress rating googleMapsUri
   <chr>        <int> <chr> <chr> <chr>               <chr>                  <chr>             <dbl> <chr>        
 1 "{\"n…           1 plac… ChIJ… (212) 221-3800      +1 212-221-3800        200 W 44th St, …    4.5 https://maps…
 2 "{\"n…           2 plac… ChIJ… (212) 302-2000      +1 212-302-2000        1515 Broadway @…    4.4 https://maps…
 3 "{\"n…           3 plac… ChIJ… <NA>                <NA>                   229 W 43rd St, …    4.8 https://maps…
 4 "{\"n…           4 plac… ChIJ… (212) 343-3355      +1 212-343-3355        1501 Broadway, …    4.4 https://maps…
 5 "{\"n…           5 plac… ChIJ… (212) 918-1330      +1 212-918-1330        1567 Broadway, …    4   https://maps…
 6 "{\"n…           6 plac… ChIJ… (332) 249-8600      +1 332-249-8600        1440 Broadway, …    4.3 https://maps…
 7 "{\"n…           7 plac… ChIJ… (212) 333-3254      +1 212-333-3254        2 Times Sq, New…    4.1 https://maps…
 8 "{\"n…           8 plac… ChIJ… (212) 581-6464      +1 212-581-6464        326 W 46th St, …    4.4 https://maps…
 9 "{\"n…           9 plac… ChIJ… (212) 921-2400      +1 212-921-2400        620 8th Ave, Ne…    4.2 https://maps…
10 "{\"n…          10 plac… ChIJ… (212) 354-5013      +1 212-354-5013        136 W 46th St, …    4.6 https://maps…
11 "{\"n…          11 plac… ChIJ… (646) 434-2448      +1 646-434-2448        575 7th Ave, Ne…    4.5 https://maps…
12 "{\"n…          12 plac… ChIJ… <NA>                <NA>                   135 W 50th St, …    3.9 https://maps…
13 "{\"n…          13 plac… ChIJ… (917) 565-9044      +1 917-565-9044        132 W 43rd St, …    4.4 https://maps…
14 "{\"n…          14 plac… ChIJ… (646) 435-0135      +1 646-435-0135        691 8th Ave, Ne…    4.4 https://maps…
15 "{\"n…          15 plac… ChIJ… (212) 869-3965      +1 212-869-3965        67 W 44th St, N…    4.2 https://maps…
16 "{\"n…          16 plac… ChIJ… (212) 597-5126      +1 212-597-5126        121 W 45th St, …    4.4 https://maps…
17 "{\"n…          17 plac… ChIJ… (212) 997-1270      +1 212-997-1270        36 W 48th St, N…    4.2 https://maps…
18 "{\"n…          18 plac… ChIJ… (212) 265-5400      +1 212-265-5400        155 W 43rd St, …    4.2 https://maps…
19 "{\"n…          19 plac… ChIJ… (212) 398-7440      +1 212-398-7440        151 W 46th St, …    4.3 https://maps…
20 "{\"n…          20 plac… ChIJ… (212) 997-4540      +1 212-997-4540        254 W 47th St, …    4.4 https://maps…
# ℹ abbreviated name: ¹​internationalPhoneNumber
# ℹ 68 more variables: websiteUri <chr>, utcOffsetMinutes <dbl>, adrFormatAddress <chr>, businessStatus <chr>,
#   priceLevel <chr>, userRatingCount <dbl>, iconMaskBaseUri <chr>, iconBackgroundColor <chr>, takeout <lgl>,
#   delivery <lgl>, dineIn <lgl>, curbsidePickup <lgl>, reservable <lgl>, servesBreakfast <lgl>,
#   servesLunch <lgl>, servesDinner <lgl>, servesBeer <lgl>, servesWine <lgl>, servesBrunch <lgl>,
#   servesVegetarianFood <lgl>, primaryType <chr>, shortFormattedAddress <chr>, outdoorSeating <lgl>,
#   liveMusic <lgl>, menuForChildren <lgl>, servesCocktails <lgl>, servesDessert <lgl>, servesCoffee <lgl>, …

更多信息请访问 https://github.com/aTnT/newplaces

© www.soinside.com 2019 - 2024. All rights reserved.