#' Filter MQT
#'
#' Filters current and previous MTH rows based on .version,
#' adds column `mqt` with values "current" and "previous".
#'
#' @param .data a valid dataframe
#' @param .date_var a date column, defaults to month
#' @param .version a date, ISO format
#'
#' @export
filter_mqt <- function(.data, .date_var, .version) {
version <- as.Date(.version)
month_col <- dplyr::as_label(rlang::enquo(.date_var))
stopifnot(lubridate::is.Date(version))
stopifnot("Supplied .date_var is not in dataframe" =
month_col %in% colnames(.data))
.data %>%
dplyr::filter(
({{ .date_var }} <= version &
{{ .date_var }} >= version - months(2)) |
({{ .date_var }} <= version - months(12) &
{{ .date_var }} >= version - months(14))
) %>%
dplyr::mutate(
mth = dplyr::case_when(
{{ .date_var }} >= version - months(2) ~ "current",
{{ .date_var }} >= version - months(14) ~ "previous",
TRUE ~ "out-of-scope"
)
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.