Skip to content

R: reduce redundency #16

Description

@zekrom-vale

There is some redundancy in the code. It should use utility and helper functions to streamline and standardize the process.

  • For each 'span.explain' element, extract the 'title' attribute and replace the element's text with the title
spans|>
	walk(~{
		# Due to pointers this modifies the original HTML source
		title = rvest::html_attr(.x, "title")  # Extract the 'title' attribute
		xml2::xml_text(.x) <- paste0(":!:", title)  # Replace the element's text with the title
	})
  • Rename the columns that match the specified patterns to "name"
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(
	ndex = coalesce(as.integer(str_extract(ndex, "[\\d+.]")), suppressWarnings(as.integer(ndex)))
)
  • The level inverse functions that use uniroot
ranges <- list(c(0, 2), c(2, 10^256))

# Find the range where e falls
for (range in ranges) {
	if (e >= exp$medium_slow(range[1]) && e <= exp$medium_slow(range[2])) {
		# Use uniroot to find the root of the function medium_slow(n) - e
		root <- uniroot(function(n) exp$medium_slow(n) - e, range)
		return(floor(root$root))
	}
}
# Return NA if no solution was found
return(NA)
  • requireNamespace functions
  • EXTRACT_REGEX = "([^()]+)(?:\\(([^()]+)\\))?"
  • Mutate across characters if "" replace with NA_character_
mutate(across(
	where(is.character),
	~ if_else(.=="", NA_character_, ., NA_character_)
))
  • Standardized column name function
  • Standard mega processing
  • Standard regional/form/name/mega processing
  • Move away from useless variables IE URL (May keep HTML or pass it to a cashing function)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions