Skip to content

Commit

Permalink
transferred check_date() and find_closest_year() function from oe…
Browse files Browse the repository at this point in the history
…li pkg to fHMM
  • Loading branch information
loelschlaeger committed Aug 5, 2024
1 parent 8a10a28 commit d3e9e7e
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 11 deletions.
4 changes: 2 additions & 2 deletions R/download_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ download_data <- function(
if (missing(symbol) || !is.character(symbol) || length(symbol) != 1) {
stop("'symbol' must be a single character.", call. = FALSE)
}
from <- oeli::check_date(from)
from <- check_date(from)
min_date <- as.Date("1902-01-01")
if (from < min_date) {
warning("'from' is set to lower bound of '1902-01-01'.", call. = FALSE)
from <- min_date
}
to <- oeli::check_date(to)
to <- check_date(to)
if (to < from) {
stop("'to' must not be earlier than 'from'.", call. = FALSE)
}
Expand Down
4 changes: 2 additions & 2 deletions R/fHMM_controls.R
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,10 @@ validate_controls <- function(controls) {
controls[["data"]] <- NA
} else {
if (!is.na(controls[["data"]][["from"]])) {
controls[["data"]][["from"]] <- oeli::check_date(controls[["data"]][["from"]])
controls[["data"]][["from"]] <- check_date(controls[["data"]][["from"]])
}
if (!is.na(controls[["data"]][["to"]])) {
controls[["data"]][["to"]] <- oeli::check_date(controls[["data"]][["to"]])
controls[["data"]][["to"]] <- check_date(controls[["data"]][["to"]])
}
if (hierarchy) {
if (is.data.frame(controls[["data"]][["file"]])) {
Expand Down
2 changes: 1 addition & 1 deletion R/fHMM_events.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fHMM_events <- function(events) {
stop("'events' must be a list containing the elements 'dates' and 'labels'.",
call. = FALSE)
}
events$dates <- oeli::check_date(events$dates)
events$dates <- check_date(events$dates)
class(events) <- "fHMM_events"
}
return(events)
Expand Down
45 changes: 45 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Check date format
#'
#' @description
#' This function checks if the input \code{date} has the format
#' \code{"YYYY-MM-DD"}.
#'
#' @param date \[`character(1)`\]\cr
#' The date in format \code{"YYYY-MM-DD"}.
#'
#' @return
#' \code{as.Date(date)} if \code{date} has the format \code{"YYYY-MM-DD"}.
#' Otherwise, the function throws an error.
#'
#' @keywords internal

check_date <- function(date) {
date <- try(as.Date(date, format = "%Y-%m-%d"), silent = TRUE)
if (inherits(date, "try-error") || anyNA(date)) {
stop("Date is not in required format 'YYYY-MM-DD'.", call. = FALSE)
}
return(date)
}

#' Find closest year
#'
#' @description
#' This function takes a date as input and returns the closest year.
#'
#' @param date \[`character(1)`\]\cr
#' The date in format \code{"YYYY-MM-DD"}.
#'
#' @return
#' An \code{integer}, the closest year to the input date.
#'
#' @keywords internal

find_closest_year <- function(date) {
year <- as.numeric(format(date, "%Y"))
ifelse(
date <= as.Date(paste0(year, "-06-30")),
year,
year + 1
)
}

8 changes: 4 additions & 4 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -548,14 +548,14 @@ plot_ts <- function(
ylim <- c(ymin - (ymax - ymin) * 2, ymax)
}
if (is.null(from)) {
xmin <- as.Date(paste0(oeli::find_closest_year(xdata[1]), "-01-01"))
xmin <- as.Date(paste0(find_closest_year(xdata[1]), "-01-01"))
} else {
xmin <- oeli::check_date(from)
xmin <- check_date(from)
}
if (is.null(to)) {
xmax <- as.Date(paste0(oeli::find_closest_year(tail(xdata, n = 1)), "-01-01"))
xmax <- as.Date(paste0(find_closest_year(tail(xdata, n = 1)), "-01-01"))
} else {
xmax <- oeli::check_date(to)
xmax <- check_date(to)
}
plot(
xdata, ydata, type = "l", xlim = c(xmin, xmax), ylim = ylim,
Expand Down
21 changes: 21 additions & 0 deletions man/check_date.Rd

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

19 changes: 19 additions & 0 deletions man/find_closest_year.Rd

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

4 changes: 2 additions & 2 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"MASS": {
"Package": "MASS",
"Version": "7.3-60.2",
"Version": "7.3-61",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -39,7 +39,7 @@
"stats",
"utils"
],
"Hash": "2f342c46163b0b54d7b64d1f798e2c78"
"Hash": "0cafd6f0500e5deba33be22c46bf6055"
},
"Matrix": {
"Package": "Matrix",
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
test_that("checks of format 'YYYY-MM-DD' for dates work", {
expect_equal(
check_date(date = "2000-01-01"),
as.Date("2000-01-01")
)
expect_error(
check_date(date = "2000-02-30"),
"Date is not in required format 'YYYY-MM-DD'."
)
expect_error(
check_date(date = "2000-13-01"),
"Date is not in required format 'YYYY-MM-DD'."
)
expect_error(
check_date(date = "01.01.2021"),
"Date is not in required format 'YYYY-MM-DD'."
)
})

test_that("finding closest year works", {
expect_equal(
find_closest_year(as.Date("2022-06-01")),
2022
)
expect_equal(
find_closest_year(as.Date("2022-06-30")),
2022
)
expect_equal(
find_closest_year(as.Date("2022-07-01")),
2023
)
expect_equal(
find_closest_year(as.Date("2022-12-31")),
2023
)
})

0 comments on commit d3e9e7e

Please sign in to comment.