diff --git a/NAMESPACE b/NAMESPACE index 1879e81..81e4caa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/filter_holidays.R b/R/filter_holidays.R new file mode 100644 index 0000000..ae1df0d --- /dev/null +++ b/R/filter_holidays.R @@ -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 +} diff --git a/R/get_holiday_types.R b/R/get_holiday_types.R new file mode 100644 index 0000000..1a1a88b --- /dev/null +++ b/R/get_holiday_types.R @@ -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")) +} diff --git a/man/filter_holidays.Rd b/man/filter_holidays.Rd new file mode 100644 index 0000000..94744b4 --- /dev/null +++ b/man/filter_holidays.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/filter_holidays.R +\name{filter_holidays} +\alias{filter_holidays} +\title{Filter holidays by category} +\usage{ +filter_holidays(data, types) +} +\arguments{ +\item{data}{a \code{data.frame} with at least the following column: \code{type}, +the category of the holiday. Typically, the output of \code{get_holidays()}.} + +\item{types}{a \code{character} with the types of holidays to keep. +See \code{get_holiday_types()}.} +} +\value{ +A \code{data.frame}, same as the input but with only holidays matching the +\code{type} category. +} +\description{ +Filter holidays by category +} +\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") +} +} diff --git a/man/get_holiday_types.Rd b/man/get_holiday_types.Rd new file mode 100644 index 0000000..616acc8 --- /dev/null +++ b/man/get_holiday_types.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_holiday_types.R +\name{get_holiday_types} +\alias{get_holiday_types} +\title{Get holidays category} +\usage{ +get_holiday_types(data) +} +\arguments{ +\item{data}{a \code{data.frame} with the following column: \code{type}, +the category of the holiday. Typically, the output of \code{get_holidays()}.} +} +\value{ +A \code{character} with the categories of holidays. +} +\description{ +Get holidays category +} +\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) +} +}