── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.0 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.1 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Attaching package: 'jsonlite'
The following object is masked from 'package:purrr':
flatten
Attaching package: 'magrittr'
The following object is masked from 'package:purrr':
set_names
The following object is masked from 'package:tidyr':
extract
Assignment 6
Intro
For this assignment, I will be working with the PokéAPI data to pull data about various Pokémon. More specifically, I will be working to show how one can pull data from the source about the different generations of Pokémon, including the new games, types and Pokémon species introduced. This will be help to pull the data as for some players, they may not have been able to play a certain generation of games due to the price of an old release or not being able to acquire the hardware necessary to play a certain game. However, with emulators rising in popularity, especially for Pokémon games, it can take under 10 minutes for anyone to acquire an emulator and the data to play any retro Pokémon games that would have been previously unplayable for most. With this document, any Pokémon fan will be able to get a better idea of what each of the nine generations of Pokémon entails!
Setup
Here we will load the default packages for this document. A good thing about the PokéAPI is that it does not require an API Key to function, meaning anyone can go at anytime and perform the same commands seen here!
Endpoint
After we have loaded our packages, we can define our endpoint, which came from the PokéAPI website
poke_endpoint <- paste("https://pokeapi.co/api/v2/generation/")API Usage
Now it is time to look at our main chunk of code which will help us to define our function and call our to the API for our desired information.
poke_get_url <-
function(name) {
poke_url_gens <-paste(poke_endpoint, name, sep = "")
poke_API <-
poke_url_gens %>%
GET()
generation <-
poke_API %>%
content(as = "text",
encoding = "UTF-8") %>%
fromJSON()
generations_info <- data.frame(
PokéID = generation$id,
Name = generation$name,
Primary_Region = generation$main_region$name,
Versions_Introduced = paste(generation$version_groups$name,
collapse = ", "),
Pokemon = paste(generation$pokemon_species$name, collapse = ", "),
Types = paste(generation$types$name, collapse = ", ")
)
return(generations_info)
}Final Result Check
Now that we have created our main function. We can now run a test on the data frame to see if we have achieved our intended results.
Generation_find <- poke_get_url("generation-vi")
print(Generation_find) PokéID Name Primary_Region Versions_Introduced
1 6 generation-vi kalos x-y, omega-ruby-alpha-sapphire
Pokemon
1 chespin, quilladin, chesnaught, fennekin, braixen, delphox, froakie, frogadier, greninja, bunnelby, diggersby, fletchling, fletchinder, talonflame, scatterbug, spewpa, vivillon, litleo, pyroar, flabebe, floette, florges, skiddo, gogoat, pancham, pangoro, furfrou, espurr, meowstic, honedge, doublade, aegislash, spritzee, aromatisse, swirlix, slurpuff, inkay, malamar, binacle, barbaracle, skrelp, dragalge, clauncher, clawitzer, helioptile, heliolisk, tyrunt, tyrantrum, amaura, aurorus, sylveon, hawlucha, dedenne, carbink, goomy, sliggoo, goodra, klefki, phantump, trevenant, pumpkaboo, gourgeist, bergmite, avalugg, noibat, noivern, xerneas, yveltal, zygarde, diancie, hoopa, volcanion
Types
1 fairy
If followed correctly, above should be our result: