data-raw/items.R

library(tidyverse)
library(httr)
library(jsonlite)

# Get API url for each item
item_get <- GET("https://pokeapi.co/api/v2/item?offset=0&limit=10000")

# Create data frame out of JSON file
item_raw <- fromJSON(rawToChar(item_get$content))$results

# Get API information for each Item
itemdex <- map(map(map(item_raw$url, GET), function(x) {
  rawToChar(x$content)
}), fromJSON)

# Add information to data frame, expand it, and choose appropriate variable names
item_index <- item_raw %>%
  select(url) %>%
  add_column(data = itemdex) %>%
  unnest_wider(data, names_repair = "minimal") %>%
  select(
    name, cost, category, attributes, held_by_pokemon, fling_power, fling_effect
  ) %>%
  rename_with(~ c(
    "item_name", "item_cost", "category", "attributes", "pokemon",
    "fling_power", "fling_effect"
  ), names(.))

# Edit formatting
item_index$fling_effect[item_index$fling_effect == "NULL"] <- NA
item_index$pokemon[item_index$pokemon == "NULL"] <- NA
item_index$attributes[item_index$attributes == "NULL"] <- NA

# Expand variables
item_index$item_name <- map_chr(1:1607, ~ item_index$item_name[[.]][[1]])
item_index$item_cost <- map_dbl(1:1607, ~ item_index$item_cost[[.]][[1]])
item_index$category <- map_chr(1:1607, ~ item_index$category[[.]][[1]])
item_index$fling_power <- map_int(1:1607, ~ item_index$fling_power[[.]][[1]])
item_index$fling_effect <- map_chr(1:1607, ~ item_index$fling_effect[[.]][[1]])
item_index$pokemon <- map(1:1607, ~ item_index$pokemon[[.]][[1]][[1]])
item_index$attributes <- map(1:1607, ~ item_index$attributes[[.]][[1]])


# Export data
usethis::use_data(item_index, overwrite = TRUE)
abbidabbers/pokemon documentation built on May 10, 2022, 3:23 p.m.