Pokemon

Why this is useful

The goal of this API call will to be so you can insert whatever pokemon you are thinking about placing inside of your team, and it will return with all of their stats so you can make the most insightful team decisions.

Setting up the API

To set up this API all we had to do was take the base url and add in our vector of pokemon we would like to be apart of our team.

#| label: Loading Library
#| echo: false

library(tidyverse) 
Warning: package 'readr' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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)  

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten
library(magrittr)  

Attaching package: 'magrittr'

The following object is masked from 'package:purrr':

    set_names

The following object is masked from 'package:tidyr':

    extract
library(httr)
base_url <- "https://pokeapi.co/api/v2/pokemon/"

team <- c("Charizard", "Gengar", "Vaporeon", "Mew", "Snorlax", "Hitmonchan")

end <- "/?json=1"

urls <- paste(base_url, team, end, sep = "")
urls[1]
[1] "https://pokeapi.co/api/v2/pokemon/Charizard/?json=1"
Poke_data <- 
  urls[1] %>% 
  GET() %>% 
  content(as = "text",
          encoding = "UTF-8") %>% 
  fromJSON() %>% 
  use_series(stats)

all_info <- function(url){
  data_raw <- 
    url %>% 
    GET() %>% 
    content(as = "text", encoding = "UTF-8") %>% 
    fromJSON()
  
  data <- 
    data_raw %>% 
    use_series(stats) %>% 
    mutate(pokemon = data_raw$name)
  
  return(data)
}
    
    all_info(urls[1])
  base_stat effort       stat.name                          stat.url   pokemon
1        78      0              hp https://pokeapi.co/api/v2/stat/1/ charizard
2        84      0          attack https://pokeapi.co/api/v2/stat/2/ charizard
3        78      0         defense https://pokeapi.co/api/v2/stat/3/ charizard
4       109      3  special-attack https://pokeapi.co/api/v2/stat/4/ charizard
5        85      0 special-defense https://pokeapi.co/api/v2/stat/5/ charizard
6       100      0           speed https://pokeapi.co/api/v2/stat/6/ charizard
poke_team <- data.frame()

for (i in seq_along(urls)) {
  poke_team <- 
   all_info(urls[i]) %>% 
    bind_rows(poke_team)
  Sys.sleep(1)
  paste("Page",i,"of",length(urls),"collected", sep = " ") %>% 
    print()
}
[1] "Page 1 of 6 collected"
[1] "Page 2 of 6 collected"
[1] "Page 3 of 6 collected"
[1] "Page 4 of 6 collected"
[1] "Page 5 of 6 collected"
[1] "Page 6 of 6 collected"