From e949cb4ea6ef470fbebcc9bfd57447f565d9702f Mon Sep 17 00:00:00 2001 From: Chantel Wetzel Date: Thu, 4 Apr 2024 07:33:06 -0700 Subject: [PATCH] refactor: add function to create species string #126 --- R/pull_bio.R | 7 +------ R/pull_biological_samples.R | 7 +------ R/pull_catch.R | 12 +----------- man/convert_to_hex_string.Rd | 31 +++++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 man/convert_to_hex_string.Rd diff --git a/R/pull_bio.R b/R/pull_bio.R index 5b4a01d..331867a 100644 --- a/R/pull_bio.R +++ b/R/pull_bio.R @@ -88,12 +88,7 @@ pull_bio <- function(common_name = NULL, ) # symbols here are generally: %22 = ", %2C = ",", %20 = " " - species_str <- paste0("%22",stringr::str_replace_all(species[1]," ","%20"),"%22") - if(length(species) > 1) { - for(i in 2:length(species)) { - species_str <- paste0(species_str, "%2C", paste0("%22",stringr::str_replace_all(species[i]," ","%20"),"%22")) - } - } + species_str <- convert_to_hex_string(species) add_species <- paste0("field_identified_taxonomy_dim$", var_name, "|=[", species_str,"]") if (any(species == "pull all")) { diff --git a/R/pull_biological_samples.R b/R/pull_biological_samples.R index 2ff81ba..07e0755 100644 --- a/R/pull_biological_samples.R +++ b/R/pull_biological_samples.R @@ -52,12 +52,7 @@ pull_biological_samples <- function(common_name = NULL, } # symbols here are generally: %22 = ", %2C = ",", %20 = " " - species_str <- paste0("%22",stringr::str_replace_all(species[1]," ","%20"),"%22") - if(length(species) > 1) { - for(i in 2:length(species)) { - species_str <- paste0(species_str, "%2C", paste0("%22",stringr::str_replace_all(species[i]," ","%20"),"%22")) - } - } + species_str <- convert_to_hex_string(species) add_species <- paste0("field_identified_taxonomy_dim$", var_name, "|=[", species_str,"]") if (any(species == "pull all")) { diff --git a/R/pull_catch.R b/R/pull_catch.R index aa6ecfa..f0b7956 100644 --- a/R/pull_catch.R +++ b/R/pull_catch.R @@ -115,17 +115,7 @@ pull_catch <- function(common_name = NULL, vars_short <- vars_long[!vars_long %in% perf_codes] # symbols here are generally: %22 = ", %2C = ",", %20 = " " - species_str <- paste0( - "%22",stringr::str_replace_all(species[1]," ","%20"),"%22" - ) - - if(length(species) > 1) { - for(i in 2:length(species)) { - species_str <- paste0( - species_str, "%2C", paste0( - "%22",stringr::str_replace_all(species[i]," ","%20"),"%22")) - } - } + species_str <- convert_to_hex_string(species) add_species <- paste0("field_identified_taxonomy_dim$", var_name, "|=[", species_str,"]") if (any(species == "pull all")) { diff --git a/man/convert_to_hex_string.Rd b/man/convert_to_hex_string.Rd new file mode 100644 index 0000000..9530aba --- /dev/null +++ b/man/convert_to_hex_string.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities.R +\name{convert_to_hex_string} +\alias{convert_to_hex_string} +\title{Utility function used throughout the package} +\usage{ +convert_to_hex_string(x) +} +\arguments{ +\item{x}{A string of either common_name or +sci_name} +} +\description{ +Utility function used throughout the package +} +\details{ +Function that converts a string to a hex string +for common name or scientific name when pulling +data. This function is used within the pull_* +functions that retrive species specific data +} +\examples{ +\dontrun{ +common_name <- c("lingcod", "sablefish", "Pacific cod") +convert_to_hex_string(common_name) +} + +} +\author{ +Kelli Johnson +}