library(tidyverse) # All the tidy things
library(jsonlite) # Converting json data into data frames
library(magrittr) # Extracting items from list objects using piping grammar
library(httr) # Interacting with HTTP verbsAssignment 6
The Dog API
The Dog API website is a website with all sorts of information pertaining to dogs. This includes images, health information, breed information, behavioral facts, and more! I will be using this website and API to create a function that creates a data frame of this information to use for further analysis.
Setting Up the API
Before we begin, we need to run our packages.
1) Get a free API key from the website: https://thedogapi.com/en
2) You can also get the base URL from the website. For student and research purposes, you are allowed to look at data regarding dog breeds. The URL endpoint can then be put into R to help us create our complete URL and function.
dogs_base_url <- "https://api.thedogapi.com/v1/breeds?api_key="
api_key <- "YOUR_API_KEY_HERE"We need a quick function that will help us call on the API once you enter your own information. There are many pieces of data we can gain from this source, but some basic parameters we can call on include:
id: unique identifier
name: dog breed
bred_for
breed_group:
life_span
temperament
origin
weight (in both imperial and metric)
height (in both imperial and metric)
reference_image_id
image (this will also require a different unique id, width, height, and url)
For this example, we will use breed group as our parameter, showing us dog breeds that are meant to “work”.
breed.groups <-"&breed_groups=Working"
per_page <- "&per_page=100"
page <- "&page=0"
dog_api_url <-
paste(dogs_endpoint,api_key,breed.groups,per_page,page,sep = "")
dog_api_url[1] "https://api.thedogapi.com/v1/breeds?api_key=live_krPUwd3HnMHgmD9AM3pTjGd2nWbxDnEKrOKkoFyceCESR9cxKOzDsr5K0b8m6WLT&breed_groups=Working&per_page=100&page=0"
dog_api_url_response <-
dog_api_url %>%
GET()
dog_api_data <-
dog_api_url_response %>%
content(as = "text",
encoding = "UTF-8") %>% # UTF-8 is the <near> universal standard
fromJSON()Using this API and new data frame, we can create visuals to better understand the data we are given. Below is a bar plot of countries where these working dogs originate from. From the plot, we can see that most “working” dogs originate from Denmark!
dog_api_data %>%
ggplot(aes(x = country_codes)) +
geom_bar() +
labs(title = "Distribution of Country Origins")