-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
160 lines (113 loc) · 5.85 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
output:
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
```{r, pkgload, echo = FALSE}
for(pkg in c("yelp", "magrittr", "dplyr", "ggmap", "tidytext")) {
suppressPackageStartupMessages(library(pkg, quiet = TRUE, character.only = TRUE))
}
```
```{r, ggmap-key, echo = FALSE}
ggmap::register_google(Sys.getenv("GOOGLE_MAPS_API_KEY"), "standard")
```
[![Travis Build Status](https://travis-ci.org/richierocks/yelp.svg?branch=master)](https://travis-ci.org/richierocks/yelp)
[![Build status](https://ci.appveyor.com/api/projects/status/2jv8csk42679foc4?svg=true)](https://ci.appveyor.com/project/richierocks/yelp)
# yelp
This R package provides access to the [Yelp Fusion API, version 3](https://www.yelp.com/developers/documentation/v3).
## Installation and setup
To use this package, you must install it and get an access token for the API.
### Package installation
To install the package, you first need *remotes* installed.
```{r, eval = FALSE}
install.packages("remotes")
```
```{r, eval = FALSE}
remotes::install_github("richierocks/yelp")
```
### Getting the client ID and client secret
To gain access to the API, you have to [register with Yelp](https://www.yelp.com/signup), log in, and [create your own app](https://www.yelp.com/developers/v3/manage_app). This takes 5 to 10 minutes of pointing and clicking. It's reasonably self-explanatory, and there are more details on the [API Authentication](https://www.yelp.com/developers/documentation/v3/authentication) page.
Once you have registered, make a note of your *client ID* and *client secret*.
### Getting and storing an access token
You use the client ID and client secret to get an access token.
```{r, get_access_token, eval = FALSE}
access_token <- get_access_token(client_id, client_secret)
```
This access token can be passed into individual calls to the API, or you can store it so the package will automatically find it. `store_access_token(access_token)` will store it for the R session, or you can use your operating system tools to set a `YELP_ACCESS_TOKEN` environment variable to store it permanently.
### Join the Developer Beta program
Some features such as business match and event search require you to sign up for the Yelp Developer Beta program. You can do this from the [Manage App](https://www.yelp.com/developers/v3/manage_app) page.
### Set your locale
By default, the package assumes that you are in the USA, and uses the locale `en_US`. You can override this for the session using `set_yelp_locale()`, or permanently by setting an environment variable named `YELP_LOCALE`.
## Usage
To search for businesses close to a specific location, call `business_search()` with a string giving a search term and either a string describing a location or numbers giving latitude and longitude. The return value is a tibble (since it has a nicer print method for wider data frames) with one business per row.
```{r, business_search}
library(yelp)
library(dplyr)
salons_in_la <- business_search("beauty salon", "los angeles")
glimpse(salons_in_la)
```
The results include the longitude and latitude of each business, so you can use [*ggmap*](https://cran.r-project.org/web/packages/ggmap) to plot their locations.
```{r, ggmap}
library(ggmap)
library(magrittr)
la <- salons_in_la %$%
c(median(longitude), median(latitude))
la_map <- get_map(la, zoom = 11)
(map_plot_of_salons_in_la <- ggmap(la_map) +
geom_point(aes(longitude, latitude), data = salons_in_la)
)
```
<!--
![Map of Los Angeles, with businesses marked as points.](man/figures/README-ggmap-1.png)
-->
You can use standard `dplyr` tools to filter for businesses that are interesting to you.
```{r, filter}
five_stars_and_cheap <- salons_in_la %>%
filter(rating == 5, price == "$")
```
`business_lookup()` gives you more detailed information about businesses, including opening hours. It costs 1 API call per business, so do your
filtering before you call it.
```{r, business_lookup}
five_stars_and_cheap_details <- business_lookup(five_stars_and_cheap)
glimpse(five_stars_and_cheap_details)
five_stars_and_cheap_details$opening_hours
```
You can also use `reviews()` to get reviews of the businesses. Again, it costs
1 API call per business. Also, you are limited to 3 reviews per business, and
the text of each is truncated. This is an API limitation, not a problem with
this R package.
```{r, reviews}
five_stars_and_cheap_reviews <- reviews(five_stars_and_cheap)
glimpse(five_stars_and_cheap_reviews)
```
In theory, these can be used with the [*tidytext*](https://cran.r-project.org/web/packages/tidytext) package to perform a sentiment analysis, though the limited amount of text is a problem.
```{r, sentiment}
library(tidytext)
(five_stars_and_cheap_sentiments <- five_stars_and_cheap_reviews %>%
select(business_id, review_id, rating, text) %>%
unnest_tokens(word, text) %>%
anti_join(get_stopwords(), by = "word") %>%
inner_join(get_sentiments("bing"))
)
```
Yelp also provide information on events. Call `event_search()` to see them.
```{r, event_search}
events_in_la <- event_search("los angeles")
glimpse(events_in_la)
```
There are some other functions with more niche usage.
- `autocomplete()` takes a search term, and returns some related search terms that Yelp understands.
- `business_match()` provides a more targetted search for a business, when you know its name and rough address.
- `event_lookup()` returns details for a specific event given its Yelp ID.
- `featured_event()` returns the Yelp-curated featured event for a location.
- `food_delivery_search()` lets you find businesses that will deliver food to a given location.
- `categories()` provides more information on the search category aliases.
- `phone_search()` lets you search for a business by phone number.