Skip to content

Commit

Permalink
Change the shortest_interval() function name to shorter_interval()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvartan committed May 23, 2021
1 parent d54c3bf commit dccc4c5
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 139 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export(sd)
export(sd24)
export(sd_overall)
export(sd_week)
export(shortest_interval)
export(shorter_interval)
export(sjl)
export(sjl_rel)
export(sjl_weighted)
Expand Down
6 changes: 3 additions & 3 deletions R/random_mctq.R
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ sampler_2 <- function(x, by, envir) {
for (i in seq(3)) { # Bias
free <- get(x$name, envir = envir)

check <- shush(shortest_interval(free, work, class = "Interval"))
check <- shush(shorter_interval(free, work, class = "Interval"))
check <- lubridate::int_end(check)
if (hms::as_hms(check) == free) break

Expand Down Expand Up @@ -980,8 +980,8 @@ sampler_3 <- function(x, y, by, envir) {
for (i in seq(3)) { # Bias
x_free <- get(x$name, envir = envir)

check_w <- shush(shortest_interval(x_work, y_work))
check_f <- shush(shortest_interval(x_free, y_free))
check_w <- shush(shorter_interval(x_work, y_work))
check_f <- shush(shorter_interval(x_free, y_free))
if (check_f >= check_w) break

sample <- clock_roll(sample_time(min = min, max = max, by = by,
Expand Down
50 changes: 25 additions & 25 deletions R/shortest_interval.R → R/shorter_interval.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' Find the shortest interval between two hours
#' Find the shorter interval between two hours
#'
#' @description
#'
#' `r lifecycle::badge("maturing")`
#'
#' `shortest_interval()` finds and returns the shortest interval between two
#' `shorter_interval()` finds and returns the shorter interval between two
#' `hms` or `POSIXt` object hours.
#'
#' `longer_interval()` do the inverse of `shortest_interval()`, _i.e_,
#' `longer_interval()` do the inverse of `shorter_interval()`, _i.e_,
#' finds the longer interval between two hours. It's just a wrapper for
#' `shortest_interval(x, y, class, inverse = TRUE)`.
#' `shorter_interval(x, y, class, inverse = TRUE)`.
#'
#' @details
#'
Expand All @@ -19,21 +19,21 @@
#' there will be always two possible intervals between them, as illustrated
#' below.
#'
#' To figure out what interval is the shortest or the longer,
#' `shortest_interval()` verify two scenarios: 1. When `x` comes before `y`; and
#' To figure out what interval is the shorter or the longer,
#' `shorter_interval()` verify two scenarios: 1. When `x` comes before `y`; and
#' 2. when `x` comes after `y`. This only works if `x` value is smaller than
#' `y`, therefore, the function will make sure to swap `x` and `y` values if the
#' latter assumption is not true.
#'
#' Because `shortest_interval()` objective is to find the shortest interval, if
#' `x` and `y` are equal, the shortest interval will have a length of 0 hours,
#' Because `shorter_interval()` objective is to find the shorter interval, if
#' `x` and `y` are equal, the shorter interval will have a length of 0 hours,
#' resulting in an interval from `x` to `x`. But, if `inverse = TRUE` or
#' `longer_interval()` is used instead, the latter condition will return a
#' interval with 24 hours of length (from `x` to `x` + 1 day).
#'
#' In cases when `x` and `y` distance themselves by 12 hours, there will be no
#' shortest or longer interval (they will have equal length). In those cases,
#' `shortest_interval()` and `longer_interval()` will return the same value
#' shorter or longer interval (they will have equal length). In those cases,
#' `shorter_interval()` and `longer_interval()` will return the same value
#' (an interval of 12 hours).
#'
#' ```
Expand All @@ -42,14 +42,14 @@
#' 06:00 22:00 06:00 22:00
#' -----|------------------|---------|------------------|----->
#' 16h 8h 16h
#' longer int. shortest int. longer int.
#' longer int. shorter int. longer int.
#'
#' day 1 day 2
#' y x y x
#' 13:00 08:00 13:00 08:00
#' -----|-------------------|-------|-------------------|----->
#' 19h 5h 19h
#' longer int. shortest int. longer int.
#' longer int. shorter int. longer int.
#'
#' x,y x,y x,y x,y
#' x y x y
Expand All @@ -75,7 +75,7 @@
#'
#' ## `class` argument
#'
#' `shortest_interval()` is integrated with [mctq::convert()]. That way you
#' `shorter_interval()` is integrated with [mctq::convert()]. That way you
#' can choose what class of object you prefer as output.
#'
#' Valid `class` values are: `"Duration"`, `"Period"`, `"difftime"`, `"hms"`,
Expand All @@ -97,7 +97,7 @@
#' @return
#'
#' * If `inverse = FALSE` (default), an object of the indicated class in
#' `class` (default: `"hms"`) with the shortest interval between `x` and `y`.
#' `class` (default: `"hms"`) with the shorter interval between `x` and `y`.
#' * If `inverse = TRUE`, an object of the indicated class in `class` (default:
#' `"hms"`) with the longer interval between `x` and `y`.
#'
Expand All @@ -109,27 +109,27 @@
#'
#' x <- hms::parse_hm("23:00")
#' y <- hms::parse_hm("01:00")
#' shortest_interval(x, y)
#' shorter_interval(x, y)
#' #> 02:00:00 # Expected
#'
#' x <- lubridate::as_datetime("1985-01-15 12:00:00")
#' y <- lubridate::as_datetime("2020-09-10 12:00:00")
#' shortest_interval(x, y)
#' shorter_interval(x, y)
#' #> 00:00:00 # Expected
#'
#' ## Vector example
#'
#' x <- c(hms::parse_hm("15:30"), hms::parse_hm("21:30"))
#' y <- c(hms::parse_hm("19:30"), hms::parse_hm("04:00"))
#' shortest_interval(x, y)
#' shorter_interval(x, y)
#' #> 04:00:00 # Expected
#' #> 06:30:00 # Expected
#'
#' ## Finding the longer interval between two hours
#'
#' x <- lubridate::parse_date_time("01:10:00", "HMS")
#' y <- lubridate::parse_date_time("11:45:00", "HMS")
#' shortest_interval(x, y, inverse = TRUE)
#' shorter_interval(x, y, inverse = TRUE)
#' #> 13:25:00 # Expected
#'
#' x <- lubridate::as_datetime("1915-02-14 05:00:00")
Expand All @@ -141,16 +141,16 @@
#'
#' x <- as.POSIXct("1988-10-05 02:00:00")
#' y <- as.POSIXlt("2100-05-07 13:30:00")
#' shortest_interval(x, y, "Interval")
#' shorter_interval(x, y, "Interval")
#' #> [1] 1970-01-01 02:00:00 UTC--1970-01-01 13:30:00 UTC # Expected
#' longer_interval(x, y, "Duration")
#' #> [1] "45000s (~12.5 hours)" # Expected
#' shortest_interval(x, y, "Period")
#' shorter_interval(x, y, "Period")
#' #> [1] "11H 30M 0S" # Expected
#' longer_interval(x, y, "hms")
#' #> 12:30:00" # Expected
shortest_interval <- function(x, y, class = "hms", inverse = FALSE,
quiet = FALSE) {
shorter_interval <- function(x, y, class = "hms", inverse = FALSE,
quiet = FALSE) {
choices <- c("Duration", "Period", "difftime", "hms", "Interval")

checkmate::assert_multi_class(x, c("hms", "POSIXct", "POSIXlt"))
Expand Down Expand Up @@ -195,7 +195,7 @@ shortest_interval <- function(x, y, class = "hms", inverse = FALSE,
shush(warning(
"Element(s) ", inline_collapse(flags), " of 'x' ",
"and 'y' have intervals equal to 12 hours, i.e., ",
"there's no shortest or longer interval ",
"there's no shorter or longer interval ",
"between the two hours (they are equal). Only one ",
"possible interval was returned.",
call. = FALSE), quiet = quiet)
Expand All @@ -207,8 +207,8 @@ shortest_interval <- function(x, y, class = "hms", inverse = FALSE,
}
}

#' @rdname shortest_interval
#' @rdname shorter_interval
#' @export
longer_interval <- function(x, y, class = "hms", quiet = FALSE) {
shortest_interval(x, y, class, inverse = TRUE, quiet = quiet)
shorter_interval(x, y, class, inverse = TRUE, quiet = quiet)
}
28 changes: 14 additions & 14 deletions R/sjl.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
#' @section Methods for computing the social jetlag:
#'
#' There are different approaches to compute the social jetlag (\eqn{SJL}). By
#' default, `sjl()` uses an approach that we call "the shortest interval
#' approach" (`"shortest"`).
#' default, `sjl()` uses an approach that we call "the shorter interval
#' approach" (`"shorter"`).
#'
#' The topics below provide a simple explanation of each method supported by
#' `sjl()`. To get a detail understating of this methods, see
Expand All @@ -90,16 +90,16 @@
#'
#' __We do not recommend using this method__, as it has many limitations.
#'
#' * `"shortest"`
#' * `"shorter"`
#'
#' This is the default method for `sjl()`. It's based on the shortest
#' This is the default method for `sjl()`. It's based on the shorter
#' interval between \eqn{MSW} and \eqn{MSF}, solving most of the issues
#' relating to \eqn{SJL} computation.
#'
#' * `"longer"`
#'
#' The `"longer"` method uses the same logic of the `"shortest"` method, but,
#' instead of using the shortest interval between \eqn{MSW} and \eqn{MSF}, it
#' The `"longer"` method uses the same logic of the `"shorter"` method, but,
#' instead of using the shorter interval between \eqn{MSW} and \eqn{MSF}, it
#' uses the longer interval between the two, considering a two-day window.
#'
#' This method may help with special contexts, like when dealing with
Expand All @@ -113,7 +113,7 @@
#' return an absolute social jetlag (default: `TRUE`).
#' @param method (optional) a string indicating which method the function must
#' use to compute the social jetlag. See the Methods section to learn
#' more (default: `"shortest"`).
#' more (default: `"shorter"`).
#'
#' @return
#'
Expand Down Expand Up @@ -174,7 +174,7 @@
#' msf <- hms::parse_hm("02:30")
#' sjl(msw, msf, abs = FALSE, method = "difference")
#' #> [1] "-60300s (~-16.75 hours)" # Expected
#' sjl(msw, msf, abs = FALSE, method = "shortest") # default method
#' sjl(msw, msf, abs = FALSE, method = "shorter") # default method
#' #> [1] "26100s (~7.25 hours)" # Expected
#' sjl(msw, msf, abs = FALSE, method = "longer")
#' #> [1] "-60300s (~-16.75 hours)" # Expected
Expand All @@ -183,7 +183,7 @@
#' msf <- hms::parse_hm("04:15")
#' sjl(msw, msf, abs = FALSE, method = "difference")
#' #> [1] "5400s (~1.5 hours)" # Expected
#' sjl(msw, msf, abs = FALSE, method = "shortest") # default method
#' sjl(msw, msf, abs = FALSE, method = "shorter") # default method
#' #> [1] "5400s (~1.5 hours)" # Expected
#' sjl(msw, msf, abs = FALSE, method = "longer")
#' #> [1] "-81000s (~-22.5 hours)" # Expected
Expand All @@ -205,8 +205,8 @@
#' #> [1] "5068.12339997292s (~1.41 hours)" # Expected
#' round_time(x)
#' #> [1] "5068s (~1.41 hours)" # Expected
sjl <- function(msw, msf, abs = TRUE, method = "shortest") {
choices <- c("difference", "shortest", "longer")
sjl <- function(msw, msf, abs = TRUE, method = "shorter") {
choices <- c("difference", "shorter", "longer")

checkmate::assert_class(msw, "hms")
checkmate::assert_class(msf, "hms")
Expand All @@ -218,8 +218,8 @@ sjl <- function(msw, msf, abs = TRUE, method = "shortest") {
out <- sum_time(msf, - msw, class = "Duration", circular = FALSE,
vectorize = TRUE)
} else {
if (method == "shortest") {
interval <- shortest_interval(msw, msf, class = "Interval",
if (method == "shorter") {
interval <- shorter_interval(msw, msf, class = "Interval",
quiet = TRUE)
} else if (method == "longer") {
interval <- longer_interval(msw, msf, class = "Interval",
Expand All @@ -241,7 +241,7 @@ sjl <- function(msw, msf, abs = TRUE, method = "shortest") {

#' @rdname sjl
#' @export
sjl_rel <- function(msw, msf, method = "shortest") {
sjl_rel <- function(msw, msf, method = "shorter") {
sjl(msw, msf, abs = FALSE, method = method)
}

Expand Down
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ reference:
- random_mctq
- raw_data
- round_time
- shortest_interval
- shorter_interval
- sum_time

articles:
Expand Down
2 changes: 1 addition & 1 deletion man/assign_date.Rd

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

2 changes: 1 addition & 1 deletion man/convert.Rd

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

2 changes: 1 addition & 1 deletion man/pretty_mctq.Rd

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

2 changes: 1 addition & 1 deletion man/qplot_walk.Rd

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

2 changes: 1 addition & 1 deletion man/random_mctq.Rd

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

2 changes: 1 addition & 1 deletion man/raw_data.Rd

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

2 changes: 1 addition & 1 deletion man/round_time.Rd

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

Loading

0 comments on commit dccc4c5

Please sign in to comment.