R/ep_search_pollutant.R

Defines functions ep_search_pollutant

Documented in ep_search_pollutant

#' Provides the code for a pollutant based on its name
#'
#' @param name A string. A portion of the name of the pollutant whose code is searched for
#' @return A tibble displaying entries in \code{pollutants_id} which contain \code{name} in their label description
#' @examples
#' ep_search_pollutant("sulphur")
#'
#' @export
#'
#' @importFrom stringr str_detect
#' @importFrom dplyr filter
#' @importFrom rlang .data

ep_search_pollutant <- function(name) {
  if (!is.character(name) | length(name) > 1) {stop("name should be a string")}

  exact_match <- europollution::pollutants_id %>%
    dplyr::filter(stringr::str_detect(tolower(.data$label), name))

  if (nrow(exact_match) == 0) {stop(paste(name, "does not appear in the list of pollutants"))}
  return(exact_match)
}
vincentbagilet/europollution documentation built on May 22, 2020, 12:07 a.m.