Yelp is a platform for business reviews and recommendations. It provides a informative dataset that’s accessible to the public via an API. There are various ways to utilize the Yelp API,including the following:
Businesses can track and analyze customer reviews to improve their services.
Developers can create location-based applications to find restaurants, bars, and other businesses.
Can explore Yelp data to extract insights like pricing about local businesses.
Sign up or log in to your Yelp account on the Yelp Developers website: Yelp Developers.
Create a new Yelp App by clicking on “Create App” in your developer dashboard.
Fill in the necessary details, including the App Name and a description.
After creating the app, you’ll be provided with an API Key.
To use the Yelp Fusion API in R, you need to install a few packages and set up your API key.
library(httr)
library(jsonlite)
Sys.setenv(YELP_API_KEY = 'YOUR_YELP_API_KEY')
base_url <- "https://api.yelp.com/v3"
yelp_api_request <- function(endpoint, params = list(), method = "GET") {
url <- modify_url(base_url, path = endpoint)
headers <- add_headers(Authorization = paste("Bearer", api_key))
response <- httr::GET(url, query = params, headers = headers)
stop_for_status(response)
content(response, "parsed")
}
search_results <- yelp_api_request("businesses/search", params = list(term = "restaurant", location = "New York"))
The yelpr package provides an easier way to perform functions. For example, we can use business_search() to search for businesses based on specific criteria. In the following example, we will retrieve the first 5 Chinese restaurants in New York:
library(yelpr)
api_key <- readLines("yelp_app_key.txt")
business_ny <- business_search(api_key = api_key,
location = 'New York',
term = "chinese",
limit = 5)
cat("Chinese Restaurants in New York:\n")
for (business in business_ny$businesses) {
cat(paste(business$name, " - ", business$location$address1, "\n"))
}
The business_search() function returns a list of businesses, and you can extract details like name, address, rating, and other yelp data.
The yelpr package also provides functions for event-related operations. For example, you can use event_search_featured() to get details of a featured event in a given location using longitude and latitude:
event \<- event_search_featured(api_key = api_key,
longitude = "-74.01385",
latitude = "40.70387")
cat("Featured Event Name: ", event$name, "\n")
This example demonstrates how to retrieve the name of a featured event.
These are simple examples of what you can do with the Yelp API, but there are a lot more.