Skip to content

Commit

Permalink
feat: filter holidays
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Apr 1, 2024
1 parent 24c37c1 commit 704c844
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(filter_events)
export(filter_holidays)
export(get_calendar)
export(get_holiday_types)
export(get_holidays)
export(get_month_name)
export(get_moon_phases)
Expand Down
63 changes: 63 additions & 0 deletions R/filter_holidays.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' Filter holidays by category
#'
#' @param data a `data.frame` with at least the following column: `type`,
#' the category of the holiday. Typically, the output of `get_holidays()`.
#'
#' @param types a `character` with the types of holidays to keep.
#' See `get_holiday_types()`.
#'
#' @return A `data.frame`, same as the input but with only holidays matching the
#' `type` category.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' ## Get holidays for United Kingdom in 2024 ----
#' holidays <- get_holidays(country = "UK", year = 2024, month = 4)
#'
#' ## Get types of holidays ----
#' get_holiday_types(holidays)
#'
#' ## Filter holidays ----
#' filter_holidays(holidays, types = "Common Local Holiday")
#' }

filter_holidays <- function(data, types) {

if (missing(data)) {
stop("Argument 'data' is required", call. = FALSE)
}

if (!is.data.frame(data)) {
stop("Argument 'data' must be a data.frame", call. = FALSE)
}

if (nrow(data) == 0) {
stop("Argument 'data' must have at least one row (holiday)",
call. = FALSE)
}

if (!("type" %in% colnames(data))) {
stop("Column 'type' (category of the holiday) is missing from 'data'",
call. = FALSE)
}

if (missing(types)) {
stop("Argument 'types' is required", call. = FALSE)
}

if (!is.character(types)) {
stop("Argument 'types' must be a character", call. = FALSE)
}

if (any(!(types %in% data$"type"))) {
stop("Some holiday types in 'types' are missing from 'data'. Please use ",
"'get_holiday_types()'", call. = FALSE)
}

data <- data[data$"type" %in% types, ]
rownames(data) <- NULL

data
}
40 changes: 40 additions & 0 deletions R/get_holiday_types.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' Get holidays category
#'
#' @param data a `data.frame` with the following column: `type`,
#' the category of the holiday. Typically, the output of `get_holidays()`.
#'
#' @return A `character` with the categories of holidays.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' ## Get holidays for United Kingdom in 2024 ----
#' holidays <- get_holidays(country = "UK", year = 2024, month = 4)
#'
#' ## Get types of holidays ----
#' get_holiday_types(holidays)
#' }

get_holiday_types <- function(data) {

if (missing(data)) {
stop("Argument 'data' is required", call. = FALSE)
}

if (!is.data.frame(data)) {
stop("Argument 'data' must be a data.frame", call. = FALSE)
}

if (nrow(data) == 0) {
stop("Argument 'data' must have at least one row (holiday)",
call. = FALSE)
}

if (!("type" %in% colnames(data))) {
stop("Column 'type' (category of the holiday) is missing from 'data'",
call. = FALSE)
}

sort(unique(data$"type"))
}
34 changes: 34 additions & 0 deletions man/filter_holidays.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/get_holiday_types.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 704c844

Please sign in to comment.