Skip to content

Commit

Permalink
feat: limit to four events by day - fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Mar 30, 2024
1 parent ab5bc00 commit 01a669f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions R/add_events.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ add_events <- function(data, year, month, palette) {
calendar <- get_calendar(year, month)
data <- filter_events(data, year, month)

check_events(data)

if (nrow(data) > 0) {

## Add color to data ----
Expand Down
40 changes: 40 additions & 0 deletions R/check_events.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' Check if the max number of events by day is < 5
#'
#' @note
#' For internal purpose only
#'
#' @noRd

check_events <- function(data) {

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

if (nrow(data) > 0) {

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

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

days <- unlist(lapply(1:nrow(data), function(i) {
as.character(seq(as.Date(data[i, "from"]), as.Date(data[i, "to"]),
by = "days"))
}))

n_events_by_day <- table(days)

if (max(n_events_by_day) > 4) {
stop("Too many events by day. For aesthetic purpose, only 4 four events ",
"by day is supported.", call. = FALSE)
}
}

invisible(NULL)
}

0 comments on commit 01a669f

Please sign in to comment.