From 8b32b2a65c2dd4efc2630024ef9450ba890a44a2 Mon Sep 17 00:00:00 2001 From: kellijohnson-NOAA Date: Tue, 23 Feb 2021 14:01:32 -0800 Subject: [PATCH] FIX(PullCatch.PacFIN): nominal code wasn't working for some species * pulled out nominal code into PullNominal.PacFIN * much better code to search for nominal code * create an issue regarding should other types of catches be included? #55 --- R/PullCatch.PacFIN.R | 37 ++++++++++++++++---------- R/PullNominal.PacFIN.R | 56 +++++++++++++++++++++++++++++++++++++++ man/PullCatch.PacFIN.Rd | 22 ++++++++++++--- man/PullNominal.PacFIN.Rd | 31 ++++++++++++++++++++++ 4 files changed, 129 insertions(+), 17 deletions(-) create mode 100644 R/PullNominal.PacFIN.R create mode 100644 man/PullNominal.PacFIN.Rd diff --git a/R/PullCatch.PacFIN.R b/R/PullCatch.PacFIN.R index 3ffc328..2e8d570 100644 --- a/R/PullCatch.PacFIN.R +++ b/R/PullCatch.PacFIN.R @@ -16,8 +16,13 @@ #' specify the species and the nominal species code without searching for #' it. This is helpful for when you are getting data for two species and #' you only want nominal catch for one or if you only wanted nominal catch. +#' Nominal species code will be searched for using [PullNominal.PacFIN] if +#' the input value for `addnominal` is `TRUE`. +#' @template verbose +#' #' @export -#' @author John R. Wallace, Kelli Faye Johnson +#' @author Kelli Faye Johnson +#' @seealso [PullNominal.PacFIN] for how nominal species codes are determined. #' @return RData frames are saved to the disk and the pulled data #' are returned as a data frame. #' * CompFT - pulled data @@ -30,7 +35,7 @@ #' PullCatch.PacFIN <- function(pacfin_species_code, username = getUserName("PacFIN"), password, savedir = getwd(), - addnominal = TRUE) { + addnominal = TRUE, verbose = FALSE) { inputcode <- pacfin_species_code @@ -38,19 +43,23 @@ PullCatch.PacFIN <- function(pacfin_species_code, if (missing(password)) { password <- readline(prompt = "Enter PacFIN password without quotes\n") } - ar <- getDB(sql.area(), - username = username, password = password) + # Find nominal code if there is one and the user hasn't passed it - if (addnominal) { - spp <- getDB(sql.species(), - username = username, password = password) - pacfin_species_code <- spp[ - grepl(paste0(collapse = "|", gsub("NOM. ", "", - spp[ - match(pacfin_species_code, spp[["PACFIN_SPECIES_CODE"]]), - "PACFIN_SPECIES_COMMON_NAME"])), - spp[, "PACFIN_SPECIES_COMMON_NAME"] - ), "PACFIN_SPECIES_CODE"] + if (addnominal[1] == TRUE) { + thenominal <- PullNominal.PacFIN( + pacfin_species_code = pacfin_species_code, + username = username, + password = password) + pacfin_species_code <- c(pacfin_species_code, na.omit(thenominal)) + if (verbose) { + message("The following nominal species codes were added: ", + knitr::combine_words(thenominal) + ) + } + } else { # Add the nominal code passed by the user unless FALSE + if (addnominal[1] != FALSE) { + pacfin_species_code <- c(pacfin_species_code, addnominal) + } } data <- getDB(sql.catch(pacfin_species_code), username = username, password = password) diff --git a/R/PullNominal.PacFIN.R b/R/PullNominal.PacFIN.R new file mode 100644 index 0000000..fbab403 --- /dev/null +++ b/R/PullNominal.PacFIN.R @@ -0,0 +1,56 @@ +#' Pull the species information table and return nominal code +#' +#' Pull the species information table with PACFIN_SPECIES_CODE and +#' PACFIN_COMMON_NAME from PacFIN to determine the nominal code for +#' a given species code. +#' +#' @template pacfin_species_code +#' @template username +#' @template password +#' @importFrom magrittr %>% +#' @return A vector of character values, where each value is a +#' PACFIN_SPECIES_CODE that pertains to the input `pacfin_species_code`. +#' `NA` is returned if no values are found. +#' +PullNominal.PacFIN <- function(pacfin_species_code, username, password) { + + spp <- getDB(sql.species(), + username = username, password = password + ) + + nom <- spp[grepl("NOM\\.", spp[,2]), ] + + thenominal <- tibble::tibble(spp) %>% + dplyr::mutate(searchname = gsub( + "BLACK AND YELLOW", + "BLACK-AND-YELLOW", + PACFIN_SPECIES_COMMON_NAME + )) %>% + dplyr::mutate(searchname = gsub( + "CALIFORNIA HALIBUT", + "CALIF HALIBUT", + searchname + )) %>% + dplyr::mutate(searchname = gsub( + "PACIFIC OCEAN PERCH", + "POP", + searchname + )) %>% + dplyr::mutate(searchname = gsub( + "(CHILIPEPPER|SQUARESPOT|VERMILION) ROCKFISH", + "\\1", + searchname + )) %>% + dplyr::mutate(nominal = purrr::map_chr(searchname, + ~paste0(grep(.x, nom[, 2], value = TRUE), collapse = "|"))) %>% + dplyr::mutate(code = purrr::map(nominal, ~{ + if (.x[1] == "") return(NA) + return(nom[grep(.x, nom[, 2]), 1]) + })) %>% + dplyr::filter(PACFIN_SPECIES_CODE == pacfin_species_code) %>% + dplyr::pull(code) %>% + unlist() + + return(thenominal) + +} diff --git a/man/PullCatch.PacFIN.Rd b/man/PullCatch.PacFIN.Rd index 401d283..45ab6bb 100644 --- a/man/PullCatch.PacFIN.Rd +++ b/man/PullCatch.PacFIN.Rd @@ -9,7 +9,8 @@ PullCatch.PacFIN( username = getUserName("PacFIN"), password, savedir = getwd(), - addnominal = TRUE + addnominal = TRUE, + verbose = FALSE ) } \arguments{ @@ -37,7 +38,19 @@ code within PacFIN, e.g., \code{pacfin_species_code = c("DOVR", "DVR1")}. Users can also input a vector to pacfin_species_code if you want to specify the species and the nominal species code without searching for it. This is helpful for when you are getting data for two species and -you only want nominal catch for one or if you only wanted nominal catch.} +you only want nominal catch for one or if you only wanted nominal catch. +Nominal species code will be searched for using \link{PullNominal.PacFIN} if +the input value for \code{addnominal} is \code{TRUE}.} + +\item{verbose}{A logical value specifying if output should be written +to the screen or not. Good for testing and exploring your data but +can be turned off when output indicates errors. +The printing doesn't affect any of the returned objects, instead, +printing makes it easier to see if the data has errors and what was +done to rectify them in the given function. +Sorry, but the default is to always print to the screen, +i.e., \code{verbose = TRUE}, so you do not miss out on anything. +This is standard practice for packages in the nwfsc-assess organization.} } \value{ RData frames are saved to the disk and the pulled data @@ -56,6 +69,9 @@ research (R), and unknown (XX) Pull Catch Data from the Comprehensive Fish Ticket table in the PacFIN database. } +\seealso{ +\link{PullNominal.PacFIN} for how nominal species codes are determined. +} \author{ -John R. Wallace, Kelli Faye Johnson +Kelli Faye Johnson } diff --git a/man/PullNominal.PacFIN.Rd b/man/PullNominal.PacFIN.Rd new file mode 100644 index 0000000..bfb7a4c --- /dev/null +++ b/man/PullNominal.PacFIN.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/PullNominal.PacFIN.R +\name{PullNominal.PacFIN} +\alias{PullNominal.PacFIN} +\title{Pull the species information table and return nominal code} +\usage{ +PullNominal.PacFIN(pacfin_species_code, username, password) +} +\arguments{ +\item{pacfin_species_code}{A character string for the PacFIN +species code that you desire to pull data for. Known also as +\code{"SPID"} in the PacFIN database if using legacy sql scripts. +An example for sablefish would be +\code{pacfin_species_code = "SABL"}.} + +\item{username}{The username you would like to use to access data from +\code{datasourcename}. The default is to generate \code{username} +from your computer name.} + +\item{password}{Your password for the \code{datasourcename}.} +} +\value{ +A vector of character values, where each value is a +PACFIN_SPECIES_CODE that pertains to the input \code{pacfin_species_code}. +\code{NA} is returned if no values are found. +} +\description{ +Pull the species information table with PACFIN_SPECIES_CODE and +PACFIN_COMMON_NAME from PacFIN to determine the nominal code for +a given species code. +}