Skip to content

Commit

Permalink
Fixes issues and resolve check warnings #11 #7
Browse files Browse the repository at this point in the history
Fixes #11 except for the nonstandard license will see to that later.
  • Loading branch information
zekrom-vale committed May 19, 2024
1 parent c0926ae commit ba31077
Show file tree
Hide file tree
Showing 40 changed files with 714 additions and 250 deletions.
7 changes: 5 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(.recursive_wrap)
export(check_h)
export(damage_base)
export(damage_random)
Expand Down Expand Up @@ -34,10 +35,14 @@ export(pokemon_damage_V)
export(randomPokemon)
export(randomPokemonGen)
export(randomType)
export(read_data)
export(recursive_wrap)
export(regionalForm)
export(relevel_matched_node)
export(stab)
export(stab_multiplyer)
export(str_before_match)
export(str_longest_match)
export(type_colors)
import(methods)
importFrom(dplyr,across)
Expand Down Expand Up @@ -85,8 +90,6 @@ importFrom(readr,read_rds)
importFrom(readr,write_csv)
importFrom(readr,write_rds)
importFrom(rlang,enquos)
importFrom(rvest,html_table)
importFrom(rvest,read_html)
importFrom(stats,runif)
importFrom(stringr,regex)
importFrom(stringr,str_detect)
Expand Down
333 changes: 264 additions & 69 deletions R/Damage.R

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion R/Family.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#' This function scrapes Pokémon evolution data from Bulbapedia and returns a data frame. If `write = TRUE`, it also writes the data frame to a csv file.
#'
#' @param write Logical, if `TRUE`, writes the data frame to a csv file. Default is `FALSE`.
#' @param path The path where the csv file will be written if `write = TRUE`. Default is `system.file("data/PokemonEvolution.csv", package = "ZekDex")`.
#' @param root The root directory where the csv file will be written if `write = TRUE`. Default is "data/".
#' @param file The name of the csv file to be written if `write = TRUE`. Default is "PokemonFamily".
#' @param fileLong The name of the long format csv file to be written if `write = TRUE`. Default is "PokemonFamilyLong".
#' @return A data frame of Pokémon evolution data.
#' @importFrom purrr map pmap_lgl
#' @importFrom dplyr select mutate filter bind_rows across everything
Expand Down
10 changes: 6 additions & 4 deletions R/Lang.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#' Generate the National Dex with Other Language Data
#'
#' This function generates a tibble of the National Dex with language information.
#'
#' @param write Logical, if `TRUE`, writes the tibble to a csv file. Default is `FALSE`.
#' @param path The path where the csv file will be written if `write = TRUE`. Default is `system.file("data/PokemonLang.csv", package = "ZekDex")`.
#' @param root The root directory where the csv file will be written if `write = TRUE`. Default is "data/".
#' @param file The name of the csv file to be written if `write = TRUE`. Default is "PokemonLang".
#' @return A tibble of the National Dex with language information.
#' @export
#'
#' @importFrom purrr map map2 reduce
#' @importFrom dplyr select left_join group_by group_modify mutate across bind_rows rename filter
#' @importFrom readr read_csv write_csv
Expand All @@ -16,6 +17,7 @@
#' @importFrom magrittr "%>%"
#' @importFrom tibble as_tibble
#' @importFrom pkgload is_loading
#' @export
gen_lang = function(write = FALSE, root = "data/", file = "PokemonLang"){
if(pkgload::is_loading()) return()
if(!requireNamespace("rvest", quietly = TRUE))stop("rvest required. Use install.packages(\"rvest\")")
Expand Down Expand Up @@ -48,7 +50,7 @@ gen_lang = function(write = FALSE, root = "data/", file = "PokemonLang"){
name_from_row(1, prepend = lang)|>
bind_rows()|>
# Discard junk columns
select(!matches(glue::glue("^{lang}(by National Pokédex|\\.{3}\\d+|X\\d+|by National|MS|English \\u2022 Japanese)")))|>
select(!matches(glue::glue("^{lang}(by National Pok\u00e9dex|\\.{3}\\d+|X\\d+|by National|MS|English \\u2022 Japanese)")))|>
# Drop all columns that are NA as some tables are not wanted
drop_all_na()|>
# Drop all na columns
Expand Down
5 changes: 3 additions & 2 deletions R/Mega.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ gen_mega = function(write = FALSE, root = "data/", file = "PokemonMega"){
map2(
HTML|>
rvest::html_elements('h3 [id^="Introduced_in_"] a')|>
rvest::html_text()|>str_remove("^Pok.mon "),
rvest::html_text()|>str_remove("^Pok\u00e9mon "),
function(x,y)mutate(x, introduced = y)
)
else if(mega == "Primal")
Expand All @@ -63,7 +63,8 @@ gen_mega = function(write = FALSE, root = "data/", file = "PokemonMega"){
bind_rows() |> # Combine all elements of 'table' into a single dataframe
select(-matches("Image")) |> # Remove the 'Image' column from the dataframe
rename_with(~"stone", matches("Mega\\s*Stone|Orb")) |> # Rename the 'Mega Stone' or 'Orb' column to 'stone'
rename(ndex = Dex, name = `Pokémon`) |> # Rename the 'Dex' column to 'ndex' and 'Pokémon' column to 'name'
rename(ndex = Dex) |> # Rename the 'Dex' column to 'ndex' and 'Pokémon' column to 'name'
rename_with(matches("Pok\u00e9mon"), ~"name")|>
rename_with(function(x){
str_replace(x, "(Type|Ability)(Before|After).*", "\\1\\2") |> # Replace 'TypeBefore', 'TypeAfter', 'AbilityBefore', and 'AbilityAfter' with 'Type' and 'Ability'
str_replace("Type|Ability", str_to_lower) # Convert 'Type' and 'Ability' to lowercase
Expand Down
16 changes: 8 additions & 8 deletions R/Pokemon.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,32 @@ scrape_pokemon = function(path = "E:\\html\\"){
#' # > {html_node}
#' # > <div>
#' }
relevel_matched_node <- function(lst, level = 1){
relevel_matched_node <- function(lst, level = 1, tags = c("h2", "h3", "h4")){
if(pkgload::is_loading()) return()
tags <- c("h2", "h3", "h4")
# If it's a node return as is
if (inherits(lst, "xml_node")) return(lst)
if (level <= length(tags) && any(purrr::map_lgl(lst, function(x) check_h(x, tags[level])))) {
if (level <= length(tags) && any(purrr::map_lgl(lst, ~check_h(.x, tags[level])))) {
# Pluck the node
both <- pluck_matched_node(lst, tags[level])
# Other elements here
other_elements <- both$other_elements
# Matched element here
matched_elements <- both$matched_elements
# Recurce, may not be necessary
return(c(matched_elements, purrr::map(other_elements, relevel_matched_node, level)))
return(c(matched_elements, purrr::map(other_elements, ~relevel_matched_node(.x, level, tags))))
} else {
# Check if an element is an xml_node or xml_nodeset before recurcing
purrr::map(lst, function(x) {
purrr::map(lst, ~{
# If it's a node return as is
if(inherits(x, "xml_node"))
return(x)
if(inherits(.x, "xml_node"))
return(.x)
# If it's a list or a nodeset recurce
return(purrr::map(x, relevel_matched_node, level))
return(purrr::map(.x, ~relevel_matched_node(.x, level, tags)))
})
}
}


#' Checks if a given xml_node type of tag exists
#'
#' @param x The object to search
Expand Down
7 changes: 4 additions & 3 deletions R/Regional.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#' This function generates a tibble of the regional Pokedex by scraping data from Bulbapedia. It extracts the URLs of the regional Pokedex pages from the HTML, reads the HTML of each regional Pokedex page, and combines the regional Pokedex data with the national Pokedex data.
#'
#' @param write Logical, if `TRUE`, writes the tibble to a csv file. Default is `FALSE`.
#' @param root The root directory where the csv file will be written if `write = TRUE`. Default is "data/".
#' @param file The name of the csv file to be written if `write = TRUE`. Default is "PokemonRegional".
#' @return A tibble of regional dexes.
#' @export
#'
#' @importFrom purrr map map2 reduce discard transpose
#' @importFrom dplyr mutate select distinct left_join join_by bind_rows rename rename_with starts_with
#' @importFrom readr read_csv write_csv
Expand All @@ -14,6 +14,7 @@
#' @importFrom tidyr drop_na
#' @importFrom magrittr "%>%"
#' @importFrom pkgload is_loading
#' @export
gen_reginal = function(write = FALSE, root = "data/", file = "PokemonRegional"){
if(pkgload::is_loading()) return()
# Check if the 'rvest' package is installed. If not, stop the function and ask the user to install it.
Expand Down Expand Up @@ -71,7 +72,7 @@ gen_reginal = function(write = FALSE, root = "data/", file = "PokemonRegional"){
ndex = as.integer(str_remove_all(ndex, "[^\\d]"))
)|>
select(!matches("^(MS|Image|ObsidianFieldlands|CrimsonMirelands|CobaltCoastlands|CoronetHighlands|AlabasterIcelands)$"))|>
rename(name = `Pokémon`)|>
rename_with(matches("Pok\u00e9mon"), ~"name")|>
rename_with(!c(ndex, name, starts_with("type")), .fn = function(.){
game = str_remove_all(dex$game, "_")
dex = str_remove_all(dex$dexes, "_")
Expand Down
8 changes: 4 additions & 4 deletions R/Stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
#' This function scrapes Pokemon statistics from Bulbapedia and returns a list of two data frames: one in long format and one in wide format. If `write = TRUE`, it also writes these data frames to csv files.
#'
#' @param write Logical, if `TRUE`, writes the data frames to csv files. Default is `FALSE`.
#' @param path The path where the csv file will be written if `write = TRUE`. Default is `system.file("data/PokemonStats.csv", package = "ZekDex")`.
#' @param pathWide The path where the wide format csv file will be written if `write = TRUE`. Default is `system.file("data/PokemonStatsWide.csv", package = "ZekDex")`.
#' @param root The root directory where the csv file will be written if `write = TRUE`. Default is "data/".
#' @param file The name of the csv file to be written if `write = TRUE`. Default is "PokemonStats".
#' @param fileWide The name of the wide format csv file to be written if `write = TRUE`. Default is "PokemonStatsWide".
#' @return A list of two data frames: PokemonStats (long format) and PokemonStatsWide (wide format).
#' @export
#'
#' @importFrom purrr map map2 reduce
#' @importFrom dplyr select rename mutate bind_rows
#' @importFrom tidyr pivot_wider
#' @importFrom readr write_csv
#' @importFrom stringr str_extract
#' @importFrom magrittr "%>%"
#' @importFrom pkgload is_loading
#' @export
gen_stats = function(
write = FALSE,
root = "data/",
Expand Down
1 change: 0 additions & 1 deletion R/Type.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#'
#' @export
#'
#' @importFrom rvest read_html html_table
#' @importFrom dplyr select filter bind_rows rename
#' @importFrom tibble tibble
#' @importFrom pkgload is_loading
Expand Down
50 changes: 30 additions & 20 deletions R/Util.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#' It returns a string that is recursively wrapped with the specified opening and closing characters.
#'
#' @param str The string to be wrapped.
#' @param .open The opening character(s) for wrapping, default is "(".
#' @param .close The closing character(s) for wrapping, default is ")?".
#' @param .open The opening character(s) for wrapping. Default is "(".
#' @param .close The closing character(s) for wrapping. Default is ")?".
#' @return A string that is recursively wrapped with the specified opening and closing characters.
#'
#' @importFrom stringr str_length str_sub
#' @importFrom glue glue
#' @examples
#' ZekDex:::.recursive_wrap("Zekrom")
#' @export
.recursive_wrap = function(str, .open="(", .close=")?"){
if(str_length(str) == 1)return(str)
return(glue::glue(
Expand All @@ -28,9 +28,14 @@

#' Vectorized version of the .recursive_wrap function
#'
#' @param str A vector of strings to be wrapped
#' This function is a vectorized version of the .recursive_wrap function. It applies the .recursive_wrap function to each element of a vector of strings.
#'
#' @return A vector of strings that are recursively wrapped with the specified opening and closing characters
#' @param str A vector of strings to be wrapped.
#' @inheritParams .recursive_wrap
#' @return A vector of strings that are recursively wrapped with the specified opening and closing characters.
#' @examples
#' ZekDex:::recursive_wrap(c("Zekrom", "Reshiram"))
#' @export
recursive_wrap = Vectorize(.recursive_wrap, vectorize.args = "str")

#' Returns the shortest string before a match
Expand All @@ -39,12 +44,15 @@ recursive_wrap = Vectorize(.recursive_wrap, vectorize.args = "str")
#'
#' @param str The string to resize.
#' @param test The string to test.
#' @param ... Additional arguments to be passed to the `regex` function.
#' @param .open The opening character(s) for wrapping in the .recursive_wrap function. Default is "(".
#' @param .close The closing character(s) for wrapping in the .recursive_wrap function. Default is ")?".
#' @return The shortest string not matching test.
#'
#' @importFrom stringr str_extract_all
#' @importFrom magrittr "%>%"
#' @examples
#' ZekDex:::str_longest_match("Zekrom", "Zek")
#' @export
str_longest_match = Vectorize(function(str, test, ..., .open="(", .close=")?"){
# args = list(...); if(!is.null(args$literal) && args$literal == TRUE)stop("Cannot match anything when literal is true")
matches = str_extract_all(test, regex(.recursive_wrap(str), ...))|>
Expand All @@ -57,14 +65,13 @@ str_longest_match = Vectorize(function(str, test, ..., .open="(", .close=")?"){
#'
#' This function returns the string before a match. It uses the `str_longest_match` function to find the match and then returns the string before it.
#'
#' @param str The string to resize.
#' @param test The string to test.
#' @inheritParams str_longest_match
#' @return The string before the match.
#'
#' @importFrom stringr str_sub
#' @importFrom dplyr if_else
#' @examples
#' ZekDex:::str_before_match("Zekrom", "Zek")
#' @export
str_before_match = function(str, test, ..., .open="(", .close=")?"){
match = str_longest_match(str, test, ..., .open="(", .close=")?")
match = if_else(match == str, NA_character_, match)
Expand All @@ -85,13 +92,13 @@ str_before_match = function(str, test, ..., .open="(", .close=")?"){
#'
#' @examples
#' # Note: This function is not exported, so we use ::: to access it
#' ZekDex:::.removeA("Pokémon") # Returns "Pokemon"
#' ZekDex:::.removeA("résumé") # Returns "resume"
#' ZekDex:::.removeA("naïve") # Returns "naive"
#' ZekDex:::.removeA("élève") # Returns "eleve"
#' ZekDex:::.removeA("hôtel") # Returns "hotel"
#' ZekDex:::.removeA("Mëtàl") # Returns "Metal"
#' ZekDex:::.removeA("Pókémòn") # Returns "Pokemon"
#' ZekDex:::removeA("Pokémon") # Returns "Pokemon"
#' ZekDex:::removeA("résumé") # Returns "resume"
#' ZekDex:::removeA("naïve") # Returns "naive"
#' ZekDex:::removeA("élève") # Returns "eleve"
#' ZekDex:::removeA("hôtel") # Returns "hotel"
#' ZekDex:::removeA("Mëtàl") # Returns "Metal"
#' ZekDex:::removeA("Pókémòn") # Returns "Pokemon"
removeA = function(str){
# Define the replacements for each accented character
replacements = c(
Expand Down Expand Up @@ -138,10 +145,10 @@ str_half = Vectorize(.str_half, vectorize.args = "str")
#' @description
#'
#' takes a vector of names, matches them against patterns using str_detect,
#' and then returns the corresponding values from patternsT[[val]]
#' and then returns the corresponding values from `patternsT[[val]]`
#' where the match is TRUE.
#'
#' @param name The value(s) to test
#' @param names_vector The value(s) to test
#' @param match What to check for a vector RegEx
#' @param val The value to return
fun = function(names_vector, match, val){
Expand Down Expand Up @@ -536,10 +543,13 @@ save_data = function(
#' @param data The name of the data to load.
#' @param root The root directory where the file is located.
#' @param ns The namespace from which to load the data.
#' @param one Is there one item in the rda file?
#'
#' @param one Logical, if `TRUE`, returns the first item in the RDA file. Default is `TRUE`.
#' @param g The name of the data to load from the namespace `ns`. If `NULL`, the function will try to load data named `data` from `ns`.
#' @return The loaded data.
#' @importFrom readr read_csv
#' @importFrom glue glue
#' @importFrom pkgload is_loading
#' @export
read_data = function(data, root, ns = asNamespace("ZekDex"), one = TRUE, g=NULL){
if(pkgload::is_loading()) return()
if(!is.null(root)){
Expand Down
2 changes: 1 addition & 1 deletion R/catch.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ gen_catch = function(write = FALSE, root = "data/", file = "PokemonCatch"){
)|>
rename(ndex = Ndex)|>
# Rename the columns that match the specified patterns to "name"
rename_with(~ "name", .cols = matches("^(Name|Pokémon)$"))|>
rename_with(~ "name", .cols = matches("^(Name|Pok\u00e9mon)$"))|>
# Mutate the 'ndex' column by attempting to extract integer values from it,
# and if that fails (returns NA), use the original 'ndex' values
mutate(
Expand Down
10 changes: 7 additions & 3 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#' \item{`Mega`}{`character` The Mega form. These are special forms that certain Pokémon can take.}
#' \item{`MegaOrPrimal`}{`factor` Indicates if the Pokémon is in a Mega or Primal form. These are special forms that certain Pokémon can take.}
#' \item{`regional`}{`factor` The regional form of the Pokémon. This refers to the variations of Pokémon that appear differently and have different attributes compared to their counterparts in other regions.}
#' \item{`form`}{`character` Form of the Pokemon. This refers to the different physical forms that Pokémon can take. NA if the Pokemon does not have a special form.}
#' \item{`HP_I`}{`integer` Hit Points for Generation I. This determines how much damage a Pokémon can receive before fainting.}
#' \item{`HP_VI`}{`integer` Hit Points for Generation VI. This determines how much damage a Pokémon can receive before fainting.}
#' \item{`HP_VII`}{`integer` Hit Points for Generation VII. This determines how much damage a Pokémon can receive before fainting.}
Expand Down Expand Up @@ -145,7 +146,8 @@
#' \item{`SpDefense`}{`integer` Special Defense strength. This determines how much damage a Pokémon can resist from special moves. This stat is not applicable for Generation I as 'Special' stat is used instead.}
#' \item{`Mega`}{`character` The Mega form. These are special forms that certain Pokémon can take.}
#' \item{`MegaOrPrimal`}{`factor` Indicates if the Pokémon is in a Mega or Primal form. These are special forms that certain Pokémon can take.}
#' \item{`regional`}{`factor` The regional form of the Pokémon. This refers to the variations of Pokémon that appear differently and have different attributes compared to their counterparts in other regions.}
#' \item{`regional`}{`factor` The regional form of the Pokémon. This refers to the variations of Pokémon that appear differently and have different attributes compared to their counterparts in other regions.}
#' \item{`form`}{`character` Form of the Pokemon. This refers to the different physical forms that Pokémon can take. NA if the Pokemon does not have a special form.}
#' }
#' @source [List of Pokémon by base stats (Generation I). In Bulbapedia.](https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_base_stats_(Generation_I))
#' [List of Pokémon by base stats (Generation II-V). In Bulbapedia.](https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_base_stats_(Generation_II-V))
Expand Down Expand Up @@ -253,7 +255,7 @@
#' \item{`Chinese_Pinyin`}{`character` The Chinese Pinyin name of the Pokémon.}
#' \item{`Chinese_Yale`}{`character` The Chinese Yale name of the Pokémon.}
#' \item{`Chinese_Former_names`}{`character` The Chinese Former names of the Pokémon.}
#' \item{`Brazilian_Portuguese_Brazilian_Portuguese`}{`character` The Brazilian Portuguese name of the Pokémon.}
#' \item{`Brazilian_Portuguese`}{`character` The Brazilian Portuguese name of the Pokémon.}
#' \item{`Turkish`}{`character` The Turkish name of the Pokémon.}
#' \item{`Russian_Cyrillic`}{`character` The Russian Cyrillic name of the Pokémon.}
#' \item{`Russian_Romanization`}{`character` The Russian Romanization name of the Pokémon.}
Expand Down Expand Up @@ -569,7 +571,9 @@
#' \item{`typeAfter`}{`factor` The primary type of the Pokémon after Mega Evolution or Primal Reversion.}
#' \item{`type2After`}{`factor` The secondary type of the Pokémon after Mega Evolution or Primal Reversion, if applicable.}
#' \item{`abilityAfter`}{`character` The ability of the Pokémon after Mega Evolution or Primal Reversion.}
#' \item{`stone`}{`character` The Mega Stone or Orb required for the Pokémon to undergo Mega Evolution or Primal Reversion. Or a note in case of Rayquaza}
#' \item{`stone`}{`character` The Mega Stone or Orb required for the Pokémon to undergo Mega Evolution or Primal Reversion. Or a note in case of Rayquaza}
#' \item{`MegaOrPrimal`}{`factor` Indicates if the Pokémon is in a Mega or Primal form. These are special forms that certain Pokémon can take.}
#' \item{`introduced`}{`factor` The game in which the Pokémon was introduced.}
#' }
#' @source [Mega Evolution. In Bulbapedia.](https://bulbapedia.bulbagarden.net/wiki/Mega_Evolution)
#' [Primal Reversion. In Bulbapedia.](https://bulbapedia.bulbagarden.net/wiki/Primal_Reversion)
Expand Down
Loading

0 comments on commit ba31077

Please sign in to comment.