R/match-helpers.R

Defines functions matches_ohlca matches_ohlcav

Documented in matches_ohlca matches_ohlcav

#' Helpers for matching common financial columns
#'
#' Extensions of the family of "select helpers" from `tidyselect` like
#' [tidyselect::contains()] and [tidyselect::matches()]. `matches_ohlca()` selects the
#' columns `open`, `high`, `low`, `close`, and `adjusted`. `matches_ohlcav()`
#' additionally matches `volume`.
#'
#' @inheritParams tidyselect::matches
#'
#' @details
#'
#' As a user, you should rarely, if ever, need to specify `vars`.
#'
#' @examples
#'
#' data(FANG)
#'
#' dplyr::select(FANG, matches_ohlca())
#'
#' FANG %>%
#'   as_tbl_time(date) %>%
#'   dplyr::group_by(symbol) %>%
#'   calculate_return(matches_ohlca())
#'
#' @rdname match_helpers
#' @export
#'
matches_ohlca <- function(vars = NULL) {
  if(is.null(vars)) vars <- tidyselect::peek_vars()
  tidyselect::matches("open|high|low|close|adjusted", ignore.case = TRUE, vars = vars)
}

#' @rdname match_helpers
#' @export
matches_ohlcav <- function(vars = NULL) {
  if(is.null(vars)) vars <- tidyselect::peek_vars()
  tidyselect::matches("open|high|low|close|adjusted|volume", ignore.case = TRUE, vars = vars)
}
DavisVaughan/tidyfinance documentation built on May 24, 2019, 8:46 p.m.