R/create_filter_expression.R

Defines functions create_filter_expression

Documented in create_filter_expression

#'create an expression to use in dplyr::filter statement
#'
#'@param landingsRecord Tibble (1 x n). Single row from landingsData
#'@param categories Character vector. Names of Variables to filter over ("YEAR","QTR","NEGEAR", etc)
#'
#'@return An expression
#'
#'@section Notes:
#'
#'Values are access from the \code{landingsRecord} using the \code{categories} and strung together to make an expression in the form of
#'labe == value & label = value etc.
#'
#'
#'@export

create_filter_expression <- function(landingsRecord,categories){

  dataF <- landingsRecord %>% dplyr::select(categories)

  filterExpression <- purrr::imap(dataF, ~ if(is.character(.x)) sprintf('%s == "%s"', .y, .x)
                           else sprintf('%s == %s', .y, .x)) %>%
    purrr::reduce(stringr::str_c,sep =" & ") %>%
    parse(text = .)

  return(filterExpression)
}
NOAA-EDAB/neusCatch documentation built on Nov. 19, 2019, 7:34 p.m.