What is the PokéAPI?
#PokéAPI is a free, public API that provides detailed data about the Pokémon universe, including Pokémon, moves, abilities, types, and more.
Why would someone use it?
To analyze Pokémon data (stats, types, evolution chains).
# Accessing the API
library(tidyverse) # All the tidy things
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.2 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── 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
library(jsonlite) # Converting json data into data frames
Attaching package: 'jsonlite'
The following object is masked from 'package:purrr':
flatten
library(magrittr) # Extracting items from list objects using piping grammar
Attaching package: 'magrittr'
The following object is masked from 'package:purrr':
set_names
The following object is masked from 'package:tidyr':
extract
library(httr) # Interacting with HTTP verbs
#unlike most API's, PokéAPI does not need an API key, You can just type in the pokemon you want, for example:
# https://pokeapi.co/api/v2/pokemon/pikachu
# Making a call to the API
# Step 1: Make the API request
url <- "https://pokeapi.co/api/v2/pokemon/pikachu"
pokemon_data <-
GET(url) %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON()
# Step 2: Getting the data
pokemon_data$name